本文介绍了了解权重和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文档可能会有所帮助:布局权重

I am trying to understand the weight layout with this example.

It is definitely not a rocket science. However, this example making it...

  • The weightSum dictate how big the size is and
  • Then divide the layout based on layout_weight value for the view in LinearLayout.

In this example I have a layout, weighted with 5, that is then divided between two views:

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

The thing that I cannot understand is the bigger number I give to ImageViewer the smallest space it get from the parent. So how it is actually calculating the size for ImageView.

You can try with the above xml. If you change the layout weight of an ImageView to 1 , and child linearlayout to 4, that I believe makes more sense, then the opposite will occur.

ImageView will expend and child linearlayout will shrink. I thought the bigger the number is more you get some space.

解决方案

Since on your outermost layout you have android:orientation="horizontal", I believe you want to vary the size/space taken by ImageView and internal LinearLayout in horizontal direction. For this case try using

android:layout_width="0dp"

on the layouts where you've put android:layout_weight. If your orientation of the outer layout was vertical, I would have used android:layout_height="0dp" in order for the weights to handle width/height of the layouts.

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

Reading over Android docs might help: Layout Weights

这篇关于了解权重和layout_weight时,layout_weight越大,布局中的收缩就越大.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:30