imageview weights in linearlayout

2019-07-29 17:39发布

问题:

I have this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:minWidth="100dp" >

    <TextView
        android:id="@+id/txt_what_way"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="4"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="3">
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>
    </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="3">
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>
    </LinearLayout>

</LinearLayout>

I want that it looks like:

  • TextView - 40% of width, Layout with ImageView - 30% of widht, Layout with - - ImageView - 30% of width.

However, the output is:

TextViewImageViewImageView----------------free space----------------------------------

回答1:

Use android:weightSum="10" in your case and Use android:background in place of android:src , replace your xml file with this

<TextView
    android:id="@+id/txt_what_way"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="4"
    android:text="test"
    android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
        android:layout_width="fill_parent"
        android:layout_height="20dp"
       android:layout_weight="3"
        android:background="@drawable/s1"/>


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:layout_weight="3"
         android:background="@drawable/s1"/>
</LinearLayout>

You might get distorted image



回答2:

This should do it ,although it would distort your image.

 <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:minWidth="100dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/txt_what_way"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:scaleType="fitCenter"
                android:src="@drawable/icon" />

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:scaleType="fitCenter"
                android:src="@drawable/icon" />

        </LinearLayout>