android - 线性布局中线条的宽度百分比

标签 android android-layout

Android 中是否有百分比宽度的选项?我试图将我的 2 行设置为 40%,将文本设置为 20%,但我真的不知道如何实现。我现在有固定宽度。

            <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="14dp"
                android:id="@+id/line2"
                android:background="@drawable/line"
                android:layout_gravity="center_horizontal"
                android:contentDescription="@string/line" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/or"
                android:textAllCaps="true"
                android:gravity="center"
                android:id="@+id/textView"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/imageView"
                android:background="@drawable/line"
                android:layout_gravity="center_horizontal"
                android:layout_weight="2.18"
                android:contentDescription="@string/line" />

        </LinearLayout>

为了更好地理解,这是来自 android studio 的屏幕:screen

最佳答案

您可以为此使用属性 android:layout_weight。 像这样:

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="5" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="14dp"
            android:id="@+id/line2"
            android:background="@drawable/line"
            android:layout_gravity="center_horizontal"
            android:layout_weight="2"
            android:contentDescription="@string/line" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/or"
            android:textAllCaps="true"
            android:gravity="center"
            android:layout_weight="1"
            android:id="@+id/textView"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/imageView"
            android:background="@drawable/line"
            android:layout_gravity="center_horizontal"
            android:layout_weight="2"
            android:contentDescription="@string/line" />

    </LinearLayout>

请注意,我已经为 LinearLayout 下的每个元素添加了一个权重,并且我已经将 LinearLayout 本身中的 android:weightSum 更改为5.

关于android - 线性布局中线条的宽度百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930482/

相关文章:

android - 允许 ListView 内的 AdMob 原生广告吗?

android - 在 Android 中实现 fragment 化时出错

java - 快速写入大文本文件

java - 测试 Android 应用程序 - 猴子中止

android - 改造滚动发光在填充之外

android - 在 LinearLayout 中嵌套 RelativeLayouts

Android 设备在设置 FPS 时崩溃,而它在 getSupportedPreviewFpsRange 范围内

android - Flutter 多平台 Android/iOS 抽屉式菜单

Android 列表项布局

Android Studio : How to quickly move styling from layout. xml 到 style.xml?