android - 相对布局的百分比宽度

标签 android android-relativelayout

我有一个布局,我只想占据屏幕宽度的一定百分比(左图)。现在,我的相对布局跨越了显示器的整个宽度(右图)。如何修复它以在左侧和右侧留出边距?

enter image description here

这就是我定义相对布局的方式:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@color/white"
    android:orientation="vertical" 
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp">

最佳答案

为此您可以使用带有 weightSum 和 layout_wright 属性的 LinearLayout

所以尝试下面的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="8" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- 1/8 % of Screen to left of your RelativeLayour -->
    </LinearLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="6"
        android:background="@color/white"
        android:orientation="vertical" >
    </RelativeLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- 1/8 % of Screen to right of your RelativeLayour -->

    </LinearLayout>

</LinearLayout>

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

相关文章:

从 Javascript 调用 Android 方法时的 Android 'CalledFromWrongThreadException'

java - 如何也按小写搜索 Firebase 文本?

android以编程方式获取按钮背景imageName

android - 应用程序启动前未收到 INSTALL_REFERRER

Android 如何从字符串中删除(“)字符

android - 动态添加内容到 RelativeLayout

java - 将相对布局中的中心 View 与相对布局之外的另一个 View 对齐

android - 在 RelativeLayout 中替代 weightSum?

android - 在 ListView 内的嵌套 RelativeLayout 中垂直居中

android - 从布局背面动画 ListView