Android:layout_weight 不适用于 Button 和 LinearLayout

祖基

我试图让按钮的大小都相同,并且都在屏幕的右侧,无论文本视图中有多少文本。

现在,按钮位于垂直 LinearLayout(由 2 个 TextView 组成)旁边,并且仅在其中一个 TextView 超过一行时才位于正确的位置。

我曾尝试将 Button 添加到 LinearLayout 并赋予它一个权重,为每个 TextViews 赋予一个权重,仅赋予文本填充的 LinearLayout 一个权重,等等都无济于事。

我现在想知道这是否是一个问题,因为这不是主要的 XML,而是一个重复的项目?但我不认为这会影响它。

这是它目前的样子

这是行项目的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Header" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

谷仓

您可以使用许多不同的布局来实现您想要的设计。这是一种方法,您可以通过将外部 Layout 元素从 更改LinearLayoutRelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/button"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:text="Button" />
</RelativeLayout>

请注意,您只需要添加属性:

android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button"

到内部LinearLayout并将此属性添加到Button视图中:

android:layout_alignParentEnd="true"

您可以添加Margin属性为视图提供一些额外的空间。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android LinearLayout layout_weight 行为

为什么相同的layout_weight导致Android ImageButton和Button的宽度不同?

Android 5.0 android:elevation适用于View,但不适用于Button?

Android 主题样式不适用于 Button

Android:自定义字体适用于TextView,不适用于Button

提交按钮不适用于Android,但适用于PC和iPhone

DrawerLayout不适用于Android 4.4和SurfaceView

app:layout_scrollFlags =“ scroll | enterAlways”不适用于LinearLayout

Android:onClick事件监听器不适用于linearlayout

适用于LinearLayout的Android分页

高程不适用于LinearLayout

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

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

VSCode中的Flutter beta热重装适用于Web,但不适用于iOS和Android(适用于Android Studio)

android:foreground属性/ setForeground()方法不适用于Button元素

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

字体不适用于CheckBox和交换机的Android工作室3

在Android Studio C ++中,代码导航和分析不适用于CMake 3.7或更高版本

Android 警报管理器不适用于特定日期和时间

adb和ifconfig命令不适用于android系统中的英特尔爱迪生

Android边距和填充不适用于ScrollViews中的RecyclerViews

使用TextView和EditText的自定义视图不适用于android:fastForward

React 不适用于 IE11 和旧版 Android 浏览器

本地化不适用于Android Emulator和Chrome

Kotlin 智能转换不适用于 LinearLayout.LayoutParams

LinearLayout水平方向不适用于RecycleView元素

Android:带有TextView和Button的onClick on LinearLayout

网站的 javascript 适用于 android、windows、linux 和所有浏览器,但不适用于任何浏览器上的 mac、ios

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