【问题标题】:Center a LinearLayout and "percentage" margins将 LinearLayout 和“百分比”边距居中
【发布时间】:2012-09-28 14:38:27
【问题描述】:

编辑:我已经收到了两个关于固定边距的综合答案。虽然我决定完全使用固定边距而不是重量边距,但最初的问题仍然悬而未决。

我正在尝试在 Android 中获得以下设计:

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

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

  • LinearLayout,垂直,作为顶级布局。如果重力设置为“居中”,则背景限制为布局的大小。另外,如何以这种方式设置百分比边距(宽度)?
  • 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>

【问题讨论】:

    标签: android android-layout margin centering android-percentrelativelayout


    【解决方案1】:

    这里是创建这种布局的最简单的xml代码检查一下

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="#000"
        android:gravity="center">
    
        <EditText android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"/>
    
        <EditText android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"/>
    
        <EditText android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"/>
    
        <EditText android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"/>
    </LinearLayout>
    

    【讨论】:

    • 虽然这确实保留了背景并消除了潜在的冗余布局,但这如何实现左右边距?
    • 只需添加这两个语句并删除 android:layout_margin="10dp", (android:layout_marginLeft="10dp" 和 android:layout_marginRight="10dp")
    • 我明白,您的回答很有用,但它不提供 procent 或基于重量的边距。 (无论如何这可能不是一个好主意;固定边距可能更好,但它与原始问题相关)。
    【解决方案2】:

    我认为相对布局是更好的选择!

    尝试阅读此主题:

    Percentage width in a RelativeLayout

    希望对你有帮助!

    【讨论】:

      【解决方案3】:
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:background="#000"
          android:gravity="center">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:background="#000"
              android:layout_gravity="center"
              android:gravity="center">
      
              <EditText android:layout_width="fill_parent"
                  android:margin_left="10dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1" />
      
              <EditText android:layout_width="fill_parent"
                  android:margin_left="10dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1" />
      
              <EditText android:layout_width="fill_parent"
                  android:margin_left="10dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1" />
      
              <EditText android:layout_width="fill_parent"
                  android:margin_left="10dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1" />
      
          </LinearLayout>
      
      </LinearLayout>
      

      试试这个

      【讨论】:

        【解决方案4】:

        您可以使用compile 'com.android.support:percent:24.2.1'

        您现在可以使用 PercentFrameLayout 或 PercentRelativeLayout。它们都具有以下属性:

        layout_widthPercent
        layout_heightPercent
        layout_marginPercent
        layout_marginLeftPercent
        layout_marginTopPercent
        layout_marginRightPercent
        layout_marginBottomPercent
        layout_marginStartPercent
        layout_marginEndPercent
        
        
        
        <?xml version="1.0" encoding="utf-8"?>
        <android.support.percent.PercentRelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <TextView
                android:id="@+id/txt1"
                android:background="#ff7acfff"
                android:text="50% - 50% margin 25"
                android:textColor="@android:color/white"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="10%"
                app:layout_heightPercent="10%"
                app:layout_widthPercent="80%" />
        
            <TextView
                android:layout_below="@id/txt1"
                android:id="@+id/txt2"
                android:background="#ff7acfff"
                android:text="50% - 50% margin 25"
                android:textColor="@android:color/white"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="10%"
                app:layout_heightPercent="10%"
                app:layout_widthPercent="80%"/>
        
            <TextView
                android:layout_below="@id/txt2"
                android:id="@+id/txt3"
                android:background="#ff7acfff"
                android:text="50% - 50% margin 25"
                android:textColor="@android:color/white"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="10%"
                app:layout_heightPercent="10%"
                app:layout_widthPercent="80%"/>
        
            <TextView
                android:layout_below="@id/txt3"
                android:id="@+id/txt4"
                android:background="#ff7acfff"
                android:text="50% - 50% margin 25"
                android:textColor="@android:color/white"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="10%"
                app:layout_heightPercent="10%"
                app:layout_widthPercent="80%"/>
        
        
        </android.support.percent.PercentRelativeLayout>
        

        【讨论】:

          【解决方案5】:

          随着 ConstraintLayout 的引入,您可以通过准则约束视图来设置视图的百分比边距。

          示例如下:

          <?xml version="1.0" encoding="utf-8"?>
          <android.support.constraint.ConstraintLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          
              <EditText
                  android:id="@+id/editText1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  app:layout_constraintBottom_toTopOf="@+id/editText2"
                  app:layout_constraintEnd_toStartOf="@+id/endGuideline"
                  app:layout_constraintStart_toStartOf="@+id/startGuideline"
                  app:layout_constraintTop_toTopOf="parent"
                  app:layout_constraintVertical_chainStyle="packed" />
          
              <EditText
                  android:id="@+id/editText2"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="20dp"
                  app:layout_constraintBottom_toTopOf="@+id/editText3"
                  app:layout_constraintEnd_toStartOf="@+id/endGuideline"
                  app:layout_constraintStart_toStartOf="@+id/startGuideline"
                  app:layout_constraintTop_toBottomOf="@+id/editText1" />
          
              <EditText
                  android:id="@+id/editText3"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="20dp"
                  app:layout_constraintBottom_toTopOf="@+id/editText4"
                  app:layout_constraintEnd_toStartOf="@+id/endGuideline"
                  app:layout_constraintStart_toStartOf="@+id/startGuideline"
                  app:layout_constraintTop_toBottomOf="@+id/editText2" />
          
              <EditText
                  android:id="@+id/editText4"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="20dp"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintEnd_toStartOf="@+id/endGuideline"
                  app:layout_constraintStart_toStartOf="@+id/startGuideline"
                  app:layout_constraintTop_toBottomOf="@+id/editText3" />
          
              <android.support.constraint.Guideline
                  android:id="@+id/startGuideline"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  app:layout_constraintGuide_percent="0.1" />
          
              <android.support.constraint.Guideline
                  android:id="@+id/endGuideline"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  app:layout_constraintGuide_percent="0.9" />
          
          </android.support.constraint.ConstraintLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-08-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-03
            • 2017-06-21
            相关资源
            最近更新 更多