安卓用户界面 : what kind of layout options are mutually exclusive?

标签 android user-interface layout attributes css

在 Android 中设计布局一段时间后,我仍然无法掌握它。结果往往是不可预测的。

我认为这部分归结为一些布局选项不能很好地协同工作。 IE。如果您使用一种指定布局的方式,则您并不打算使用其他方式。

值得注意的是,对我而言,将 layout_widthfill_parent 更改为 wrap_content 通常会改变世界。再举一个具体的例子:

我的 Activity 布局的一部分是 LinearLayout:

    <LinearLayout android:id="@+id/ll2" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">
        <LinearLayout android:id="@+id/ll2a" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1">
            <TextView android:id="@+id/l_cover" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/book_item_l_cover">
            </TextView>
            <ImageButton android:id="@+id/i_cover"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:scaleType="centerInside" android:gravity="center"
                android:layout_weight="1" android:adjustViewBounds="true"
                android:src="@drawable/spinner_black_76" android:minWidth="400dip">
            </ImageButton>
        </LinearLayout>
        <LinearLayout android:id="@+id/ll2b" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1">
            <TextView android:id="@+id/l_back_cover" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="@string/book_item_l_back_cover">
            </TextView>
            <ImageButton android:id="@+id/i_back_cover"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:scaleType="centerInside" android:gravity="center"
                android:layout_weight="1" android:adjustViewBounds="true"
                android:src="@drawable/spinner_black_76">
            </ImageButton>
        </LinearLayout>
    </LinearLayout>

要让 2 列左对齐的标签按钮组合彼此相邻,我必须使用 layout_weight 属性。由于 adjustViewBounds 属性,ImageButtons 在图像周围收缩。这让我得到了一个很好的结果。

现在,我最后还想要的是拥有一个最小尺寸的按钮,这样手指就可以轻松按下它们,即使它们不包含任何图像(它们会缩小到只有几毫米)。如您所见,我尝试设置 android:minWidth="400dip"。我预计按钮会填满整个屏幕的一半宽度(400 是一个很大的值),但我没有看到任何差异。

我想上面的其他属性之一正在阻止 minWidth 产生任何影响。我可能会去试验并禁用其他一些,看看它能给我带来什么。但我的问题是:是否有人有关于哪个属性阻止或反直觉影响另一个的任何(逻辑)信息?我想一劳永逸地解决这个问题。这在某处记录了吗?

最佳答案

我注意到 layout_weight 似乎使 minWidth 变得无用。

给定线性布局中的三个按钮,左侧和右侧的权重为 0,中心为 1,更改其中任何一个的 minWidth 对呈现 View 的方式绝对没有影响,直到我删除layout_weight 属性完全针对每个按钮。我什至确保所有 minWidth 的总和小于整个 View 的宽度。

我不知道为什么会这样。 documentation关于 layout_weight:

This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight

我也很想知道其他哪些布局参数相互冲突。为 View 定义权重以填充“父 View 中的剩余空间”对我来说似乎非常不直观,这将完全忽略 View 的 minWidth 设置。

关于安卓用户界面 : what kind of layout options are mutually exclusive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3885886/

相关文章:

android - NotificationBar 条目未在定义的时间出现

java - 寻求有关布局选项的建议

css - 动态(流体)间隔的多行元素(如对齐)

JavaFX 在 HBox 的两个侧面板之间均匀分割空间

java - Android ActionBar 选项卡布局不是全宽

android - 检查应用程序是否正在运行 - Kotlin

android - 如何在没有主机的情况下自动化android

android:当用户触摸 fragment 外部时,如何关闭 fragment ?

user-interface - 给定背景颜色的良好文本前景色

macos - Mac OS 中的多个选项卡使用哪个库