【问题标题】:Android how do I set a percentage padding/margin so EditText has 10% margin on either side?Android 如何设置百分比填充/边距,以便 EditText 两边都有 10% 的边距?
【发布时间】:2011-08-25 04:35:20
【问题描述】:

android 是否支持 % 或者有办法近似。我有两个非常不同的屏幕尺寸,但我希望活动中的 EditBox 的屏幕尺寸与屏幕尺寸的比例具有相同的边距。如何才能做到这一点。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    它并不真正支持按百分比设置值(除了一些 xml 动画文件似乎)那些由你的小数,并用 setMargin() 或 setPadding() 设置结果。

    【讨论】:

      【解决方案2】:

      这在 XML 中可以通过将 EditText 包装在 LinearLayout 中来实现:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:weightSum="10"
          android:gravity="center"
          >       
      
          <EditText
              android:layout_height="wrap_content"
              android:layout_width="0dip"
              android:layout_weight="8"
              />
      
      </LinearLayout>
      

      【讨论】:

      • 如果LinearLayout 中只有一个组件,它将无法工作。
      • 这适用于我,在 LinearLayout 中只有一个 ListView,在带有 minSdkVersion = 16 的 LinearLayout 中添加或不添加 android:orientation="horizontal"。对于高度,您只需 layout_height =
      • 对于高度,LinearLayout 中的元素只有layout_height = "0dp"(而不是 layout_width)。从理论上讲,您可以将两者嵌套以同时执行宽度和高度,尽管 Android Studio 会警告您嵌套权重不是最佳的(但我总是忽略这一点)
      【解决方案3】:

      编辑:此答案中的布局现已弃用。使用Eugene Brusov 建议的the solution。另外,感谢chancyWu 的评论。


      现在支持库版本 23.0.0 出现了一种更好的方法(大约是时候了,对吧?)。您现在可以使用PercentFrameLayoutPercentRelativeLayout

      如果您希望 EditText 占屏幕宽度的 80%,两边留出 10% 的边距,代码如下所示:

      <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">
      
           <EditText
               android:layout_height="wrap_content"
               app:layout_widthPercent="80%"
               app:layout_marginStartPercent="10%"
               app:layout_marginEndPercent="10%"/>
      
      </android.support.percent.PercentRelativeLayout>
      

      你也可以看看PercentLayoutHelper.PercentLayoutParams

      【讨论】:

      • 自 API 级别 26.1.0 起已弃用
      【解决方案4】:

      ConstraintLayout 中引入Guideline 是可能的。

      例如,您可以将 EditText 顶部放置在屏幕高度的 25% 处,左右放置在屏幕宽度的 20% 处:

      这是布局源代码:

      <androidx.constraintlayout.widget.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/editText"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="textPersonName"
              android:text="Name"
              app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
              app:layout_constraintRight_toLeftOf="@+id/right_guideline"
              app:layout_constraintTop_toTopOf="@+id/top_guideline" />
      
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/top_guideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintGuide_percent="0.25" />
      
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/left_guideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              app:layout_constraintGuide_percent="0.2" />
      
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/right_guideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              app:layout_constraintGuide_percent="0.8" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

      • 这应该是公认的答案。可以通过使用布局权重来定义,但这是非常有限的,并且 ConstraintLayouts 给你更多的控制权。
      • 最低 API 版本是多少?
      【解决方案5】:

      我在这种情况下所做的是将视图放在水平 LinearLayout 中,添加一个空视图(在主视图或布局之前或之后,具体取决于您希望边距位于左侧还是右侧)视图的宽度为 0dp,然后将空视图的权重值设置为“1”,而我将需要边距的视图/布局的权重值设置为“4”。

      这导致了 1/5 (20%) 的左边距。

      您也可以将其应用于顶部和底部边距/填充。

       <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          >
      
          <View
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1" />
      
          <LinearLayout
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:layout_weight="4"
          >
          
          <!--Your content goes here -->
      
          </LinearLayout>
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-21
        • 2012-06-30
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 2011-06-26
        • 2016-01-10
        • 1970-01-01
        相关资源
        最近更新 更多