【问题标题】:How to fix layout_weight in Linear layout?如何修复线性布局中的 layout_weight?
【发布时间】:2016-12-11 00:18:20
【问题描述】:

在我的 xml 文件中,我使用线性视图和 layout_weights 将每个小圆圈 (ImageView) 设置为屏幕 25% 的大小。
这是我手机的屏幕截图(应该是这样的):

在我的平板电脑上,它不会将大小更改为屏幕的 25%,这就是它的外观:

我的代码由三个 LinearLayouts 组成,每个 LinearLayouts 都包含权重为 0.25 的 ImageViews。
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView12"
        android:src="@drawable/circle_big"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        >

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.03125">
        </FrameLayout>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:id="@+id/imageView12"
            android:src="@drawable/circle_white"
            android:onClick="changeToScreenSelectLayout"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.09375">
        </FrameLayout>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:id="@+id/imageView12"
            android:src="@drawable/circle_white"
            android:onClick="changeToScreenSelectLayout"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.09375">
        </FrameLayout>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:id="@+id/imageView12"
            android:src="@drawable/circle_white"
            android:onClick="changeToScreenSelectLayout"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.03125">

        </FrameLayout>


    </LinearLayout>
    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:rotation = "120"
        >

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.03125">
        </FrameLayout>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:id="@+id/imageView12"
            android:src="@drawable/circle_white"
            android:onClick="changeToScreenSelectLayout"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.4375">
        </FrameLayout>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:id="@+id/imageView12"
            android:src="@drawable/circle_white"
            android:onClick="changeToScreenSelectLayout"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.03125">

        </FrameLayout>


    </LinearLayout>
    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:rotation = "60"
        >

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.03125">
        </FrameLayout>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:id="@+id/imageView12"
            android:src="@drawable/circle_white"
            android:onClick="changeToScreenSelectLayout"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />
        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.4375">
        </FrameLayout>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:id="@+id/imageView12"
            android:src="@drawable/circle_white"
            android:onClick="changeToScreenSelectLayout"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.03125">

        </FrameLayout>


    </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 使用 PercentRelativeLayout 可能会有所帮助。
  • 据我所知,布局权重必须是整数
  • @cricket_007 错误。它们可以是浮点值。
  • @cricket_007 正如here 定义的那样,权重是floats
  • 是的....人们倾向于假设数字总和为 1。但它只是权重,1:1:1 会产生与 2.5:2.5 相同的精确结果: 2.5

标签: android xml android-layout format android-linearlayout


【解决方案1】:

您不能使用 png 文件作为您的 circle_white。如果您选择正确的 android:scaleType,从技术上讲,您可以完成这项工作,但它会导致可怕的混叠渲染。

我会首先删除您现有的 png(来自每个资源存储桶),因为它们不能用于此目的。然后在drawable中创建一个名为“circle_white.xml”的新文件。把它放进去,这将画一个没有内在大小的圆圈。 IE。它只会填充你渲染它的任何东西。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#fff"/>
</shape>

然后从使用带有circle_white src 的ImageView 更改为背景设置为这个新的@drawable/circle_white 的HeightMatchesWidthView。您需要确保视图高度与我在下面的视图扩展中所做的宽度相匹配。要使用它,只需在某处创建一个新类并将其粘贴。在 xml 中引用它,而不是让 ImageView 执行 com.whatever.HeightMatchesWidthView ,其中 com.whatever 是您放入的任何包。

public class HeightMatchesWidthView extends View {
    public HeightMatchesWidthView(final Context context) {
        super(context);
    }

    public HeightMatchesWidthView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public HeightMatchesWidthView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        setMeasuredDimension(width, width);
    }

    @Override
    protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
        super.onSizeChanged(w, w, oldw, oldh);
    }
}

您也可以使用 PercentRelativeLayout 代替 HeightMatchesWidthView,并使用它来强制它们为正方形,并使用正确的大小,而无需扩展 View 以添加对强制正方形的支持。

【讨论】:

  • 是图片中的白圈
  • 所以它是一个真正的 PNG 文件?
  • 是的,它是一个从 ldpi 到 xxhdpi 的 png 文件
  • @sazzy4o 请看这篇帖子stackoverflow.com/a/34724737/2308683
  • 我会尝试将其更改为视图
猜你喜欢
  • 1970-01-01
  • 2011-10-18
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多