简体   繁体   English

如何使我的 Android LinearLayout 权重与值 1 和 2 一起工作?

[英]How can I make my Android LinearLayout weights work with values of 1 and 2?

I'm trying to make a LinearLayout that has three rows, each of equal height.我正在尝试制作一个具有三行的 LinearLayout,每行的高度相同。 I am making this using 3 LinearLayouts inside the main one, each with a layout_weight of 1.我在主布局中使用了 3 个 LinearLayout,每个布局的权重为 1。

The middle LinearLayout should contain an ImageView, TextView, ImageView, with weights of 1,2,1.中间的 LinearLayout 应该包含一个 ImageView、TextView、ImageView,权重为 1、2、1。 To do this, inside the middle LinearLayout I created another 3.为此,我在中间的 LinearLayout 中创建了另一个 3。

I want the TextView to be double the size of each ImageView.我希望 TextView 的大小是每个 ImageView 的两倍。 If you see the code below, when layout_weight is 1 for the inner LinearLayouts I have no problems, the three appear of equal size.如果您看到下面的代码,当内部 LinearLayouts 的 layout_weight 为 1 时,我没有问题,这三个出现的大小相等。 However when I set the middle one (with id=textLayout) to layout_weight=2 , it completely disappears (as seen in the Eclipse graphical layout viewer) and only the two others are visible.但是,当我将中间一个(id=textLayout)设置为layout_weight=2时,它会完全消失(如 Eclipse 图形布局查看器中所示),只有另外两个可见。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/fondo"
    android:gravity="center_vertical|center_horizontal">

    <LinearLayout
        android:id="@+id/LayoutTop"
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LayoutMid"
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="1">
            <ImageView
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">
            </ImageView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/textLayout"
            android:orientation="horizontal"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="2">
            <TextView
                android:text="Status"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="1">
            <ImageView
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">
            </ImageView>
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LayoutBottom"
        android:orientation="horizontal"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

</LinearLayout>

You don't need the additional layouts to space the middle row appropriately, and your weights are backwards.您不需要额外的布局来适当地间隔中间行,并且您的权重是向后的。 If you want the text to be 2x the outer images, you want it to 1, and the outer columns to be 1.5如果你希望文本是外部图像的 2 倍,你希望它是 1,外部列是 1.5

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout android:id="@+id/LayoutTop"
        android:orientation="horizontal" android:layout_height="0dp"
        android:layout_width="fill_parent" android:layout_weight="1" android:background="#ffff0000"/>


    <LinearLayout android:id="@+id/LayoutMid"
        android:orientation="horizontal" android:layout_height="0dp"
        android:layout_width="fill_parent" android:layout_weight="1">

        <ImageView android:layout_height="fill_parent"
            android:layout_width="fill_parent" android:background="@drawable/icon"
            android:layout_weight="1.5" />
        <TextView android:layout_height="fill_parent" android:id="@+id/TextView01"
            android:layout_weight="1" android:text="Status" android:layout_width="fill_parent" />
        <ImageView android:layout_height="fill_parent"
            android:layout_weight="1.5" android:background="@drawable/icon"
            android:layout_width="fill_parent" />
    </LinearLayout>

    <LinearLayout android:id="@+id/LayoutBottom"
        android:orientation="horizontal" android:layout_height="0dp"
        android:layout_width="fill_parent" android:layout_weight="1" android:background="#ff00ff00"/>


</LinearLayout>

I used an icon (if you have one) to put a picture in the middle row, and some colors in the top and bottom in to show the spacing more clearly.我用了一个图标(如果你有的话)在中间一行放了一张图片,并在顶部和底部放了一些 colors 以更清楚地显示间距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM