首页 > 解决方案 > 如何在 xml 布局中拉伸 Imagview 并在其左侧显示其他按钮

问题描述

我在 cardview 布局中使用 Relativelayout 来显示用户详细信息。但我想在最后显示两个按钮,宽度相等,图像左侧的所有内容都将从上到下延伸。

但我不能这样做。

这是我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:layout_marginBottom="2dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="8dp"
    >


  <ImageView
        android:id="@+id/contact_profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#fff"
        android:src="@drawable/binil"
        android:padding="1dp" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/top"
    android:padding="5dp">



    <TextView
        android:id="@+id/contact_name"
        android:layout_width="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Sagar Rawal"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/contact_mobile"
        android:text="9868336847"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/contact_name"
        android:layout_alignParentLeft="true"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/contact_address"
        android:layout_below="@+id/contact_name"
        android:layout_width="wrap_content"
        android:text="Jumla"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textStyle="bold"
        android:layout_toRightOf="@+id/contact_mobile" />


    <TextView
        android:id="@+id/contact_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="searchbbc1881@gmail.com"
        android:layout_below="@+id/contact_mobile"
        android:layout_alignParentLeft="true"
        android:textStyle="bold" />





        <ImageButton
          android:layout_below="@+id/contact_email"
            android:id="@+id/call"
            android:background="@drawable/shape_button"
            android:layout_width="wrap_content"
             android:src="@drawable/ic_call_black_24dp"
            android:layout_height="wrap_content" />

           <ImageButton
               android:layout_toRightOf="@id/call"
               android:layout_below="@+id/contact_email"
                 android:background="@drawable/shape_button"
                  android:layout_width="wrap_content"
               android:src="@drawable/ic_email_black_24dp"

            android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>

我的输出是 在此处输入图像描述

但我想要类似的东西 在此处输入图像描述

其中两个按钮将同样拉伸到 Image 的左侧。

请帮忙

标签: javaandroidandroid-layoutlayout

解决方案


尝试像这样使用 LinearLayout weightSum

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="50"
        android:layout_height="wrap_content"
        android:text="text1!" />

    <TextView
        android:layout_width="0dp"
        android:layout_weight="50"
        android:layout_height="wrap_content"
        android:text="text2!" />
</LinearLayout>

更新:用这个替换你的代码,它会解决你的问题

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:padding="5dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="8dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <RelativeLayout
            android:id="@+id/top"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".7"
            android:padding="5dp">

            <TextView
                android:id="@+id/contact_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Sagar Rawal"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/contact_mobile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/contact_name"
                android:text="9868336847"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/contact_address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/contact_name"
                android:layout_marginLeft="15dp"
                android:layout_toRightOf="@+id/contact_mobile"
                android:text="Jumla"
                android:textStyle="bold" />


            <TextView
                android:id="@+id/contact_email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/contact_mobile"
                android:text="searchbbc1881@gmail.com"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/contact_email"
                android:weightSum="1">

                <ImageButton
                    android:id="@+id/call"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/contact_email"
                    android:layout_weight=".5"
                    android:background="@drawable/shape_button"
                    android:src="@drawable/ic_email_black_24dp"
                    />

                <ImageButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/contact_email"
                    android:layout_toRightOf="@id/call"
                    android:layout_weight=".5"
                    android:background="@drawable/shape_button"
                    android:src="@drawable/ic_email_black_24dp"
                    />
            </LinearLayout>

        </RelativeLayout>

        <ImageView
            android:id="@+id/contact_profile"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".3"
            android:background="#fff"
            android:padding="1dp"
            android:src="@drawable/ic_launcher_background" />
    </LinearLayout>

</android.support.v7.widget.CardView>

推荐阅读