How to stretch Imagview in xml layout and show other button to left of it

时间:2018-09-18 20:16:36

标签: java android android-layout layout

I'm using Relativelayout within cardview layout to show the user details. But I want to show the two button at the end with equal width on and everything in the left side of Image which will stretch from Top to bottom.

But I'm not able to do so.

here is my xml code

<?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>

My output is enter image description here

But I want something similar to this enter image description here

Where two buttons will equally stretch to left side of Image.

Please help

3 个答案:

答案 0 :(得分:1)

尝试像这样使用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>

答案 1 :(得分:0)

In order to achieve that you can put your both buttons inside a linear layout and position it according to design then give it the orientation horizontal and weightSum of 1.0 . Then after that give layout weight to both the buttons of 0.5 and 0.5.

答案 2 :(得分:0)

尝试通过这种方式进行重组:

enter image description here