小编Tot*_*tus的帖子

将LinearLayout和"百分比"边距居中

编辑:我已收到两个关于固定保证金的综合答案.虽然我已经决定使用固定边距而不是重量边距,但最初的问题仍未解决.

我想在Android中获得以下设计:

期望的布局

一个居中的垂直列表(TextViews,EditViews等),它留下大约10%的水平空间作为左/右边距,带有背景.

我尝试过但没有工作/部分工作的内容:

  • LinearLayout,vertical,作为顶级布局.如果重力设置为"居中",则背景将限制为布局的大小.另外,如何以这种方式设置百分比边距(宽度)?
  • RelativeLayout上的LinearLayout:背景有效,水平居中工作,权重不存在.
  • LinearLayout上的LinearLayout:背景工作,权重工作,水平居中将所有可用空间推向右侧.

(在最后两种情况下,我的Eclipse也抱怨其中一个布局是多余的.)

我没有发布代码,认为这更像是一个与原则相关的问题.实现这一目标的最佳方法是什么?

谢谢.

对应于最后一个测试用例的XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:weightSum="1.0"
    android:background="#013c57" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Stuff -->
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android margin centering android-layout android-percentrelativelayout

6
推荐指数
1
解决办法
9617
查看次数