【问题标题】:understanding Layout_Weight of linear layout理解 Layout_Weight 的线性布局
【发布时间】:2015-01-03 22:29:14
【问题描述】:

我正在尝试创建一个布局,该布局具有一个图像视图和另一个包含 2 个文本视图的 LinearLayout。我希望 imageView 占据总布局高度的 60% 以及由 textViews 的布局获取的剩余空间。然而问题是图像视图的高度没有成为总高度的 60%。它随着图像的尺寸而变化。我一直无法弄清楚这种行为背后的原因。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myView”
    android:layout_width="@dimen/my_width"
    android:layout_height="match_parent"
    android:layout_marginBottom="6dp"
    android:orientation="vertical"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1.0"
        android:orientation="vertical" >


            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_alignParentTop="true"
                android:scaleType="centerCrop"
                android:layout_weight=“0.6”
                android:src="@drawable/ic_launcher"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.40”
            android:layout_below="@+id/imageLayout"
            android:orientation="vertical"
             >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="7dp"
                android:maxLines="1"
                android:paddingLeft="@dimen/my_padding"
                android:paddingRight="@dimen/my_padding"
                android:text=“hello “world/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="20dp"
                android:paddingLeft="16dp"
                android:text=“ty”/>


                <View
                    android:layout_width="0dp"
                    android:layout_height=“match_parent"
                    android:layout_weight="1"
                    ></View>
                <Button
                    android:layout_width="@dimen/but_width"
                    android:layout_height="@dimen/but_height"
                    android:layout_gravity="right|bottom"
                    android:text=“button”

                    />

                </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    </FrameLayout>

最好的问候

【问题讨论】:

    标签: android android-linearlayout android-layout-weight


    【解决方案1】:

    视图的宽度和高度不仅仅取决于 layout_weight 属性。例如,它可以取决于其孩子的大小(如果它是一个视图组)。您必须始终避免使用嵌套布局,因为它使用更多内存。您可以使用一种相对布局来代替框架布局和多个线性布局。

    【讨论】:

      猜你喜欢
      • 2016-12-11
      • 2011-10-18
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      相关资源
      最近更新 更多