imageview weights in linearlayout

本秂侑毒 提交于 2019-12-24 08:56:22

问题


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>


来源:https://stackoverflow.com/questions/12164814/imageview-weights-in-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!