我有一个布局,我只想占据屏幕宽度的一定百分比(左图)。现在,我的相对布局跨越了显示器的整个宽度(右图)。如何修复它以在左侧和右侧留出边距?
这就是我定义相对布局的方式:
<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/