了解权重和layout_weight时,layout_weight越大,布局中的收缩就越大。

基于代码

我试图通过此示例了解重量布局。

绝对不是火箭科学。然而,这个例子使它...

  • weightSum决定大小,然后
  • 然后根据LinearLayout中视图的layout_weight值划分布局。

在此示例中,我有一个布局,权重为5,然后将其分为两个视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transactionRowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:layout_weight="2" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="Test Title"
            android:textColor="@color/textColor"
            android:textSize="@dimen/subHeadingTextSize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="This is a test description"
            android:textColor="@color/textColor"
            android:textSize="@dimen/normalTextSize" />

    </LinearLayout>


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="top"
        android:contentDescription="" />

</LinearLayout>

我无法理解的是,我给ImageViewer更大的数字是它从父级获得的最小空间。因此,它实际上是如何计算ImageView的大小的。

您可以尝试使用上述xml。如果您将ImageView的布局权重更改为1,并将子linearlayout更改为4,我认为这更有意义,那么将发生相反的情况。

ImageView将扩大,子级linearlayout将缩小。我认为数字越大,您获得的空间就越大。

肖比·普里

由于在最外面的布局上有android:orientation="horizontal",我相信您想在水平方向上改变ImageView内部的尺寸/空间LinearLayout对于这种情况,请尝试使用

android:layout_width="0dp"

在您放置的布局上android:layout_weight如果外部布局的方向是垂直的,那么我将使用android:layout_height="0dp"权重来处理布局的宽度/高度。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transactionRowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:layout_weight="2" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="Test Title"
            android:textColor="@color/textColor"
            android:textSize="@dimen/subHeadingTextSize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="This is a test description"
            android:textColor="@color/textColor"
            android:textSize="@dimen/normalTextSize" />

    </LinearLayout>


    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="top"
        android:contentDescription="" />

</LinearLayout>

阅读Android文档可能会有所帮助:版式权重

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何修复线性布局中的layout_weight?

RelativeLayout中的无效布局参数:layout_weight

当我向布局中添加视图时,layout_weight发生更改(Android)

layout_weight重叠视图

layout_weight如何工作?

具有不同布局类型的layout_weight

带有`layout_weight`和`maxWidth`的灵活水平布局

RecyclerView中项目的layout_weight和layout_gravity不起作用

与宽度对齐时LinearLayout layout_weight =“ 1”需要单位

使用layout_weight在textview中自动水平滚动

layout_weight无法正常使用editfield

定义layout_weight为水平?

android- layout_weight =“ 0”的含义

使用layout_weight进行滚动视图

TableLayout,android:layout_weight,并包括

具有RelativeLayout的android layout_weight

Android LinearLayout layout_weight 行为

使用weightSum和layout_weight时如何在LinearLayout元素上设置最小宽度

android:layout_weight在方向更改时未从正确的布局中选择值

Android在垂直LinearLayout中将layout_weight设置为宽度

固定宽度的Android Layout_Weight不起作用

如何以编程方式设置layout_weight?

如何通过代码动态设置layout_weight属性?

android:layout_weight是什么意思?

RecyclerView项目layout_weight,尺寸未更新

Android Layout_Weight属性不起作用

在LinearLayout上显示“ android:layout_weight”时,LinearLayout内的ImageView不显示

将layout_weight分配给微调器时,微调器上的SetSelection崩溃

使用wrap_content时不显示内容,也不使用layout_weight