Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何设置填充/边距百分比,使EditText两边都有10%的边距?_Android_Android Layout - Fatal编程技术网

Android如何设置填充/边距百分比,使EditText两边都有10%的边距?

Android如何设置填充/边距百分比,使EditText两边都有10%的边距?,android,android-layout,Android,Android Layout,android是否支持%或者是否有一种近似的方法。我有两个非常不同的屏幕大小,但就百分比而言,我希望活动中的EditBox在两个屏幕大小中具有相同的边距,作为屏幕大小的比例。如何做到这一点 它实际上不支持按百分比设置值(某些xml动画文件似乎除外),如果您死心塌地地使用百分比,我能想到的最好方法是从java调用getWidth和getHeight,然后将它们乘以小数,并使用setMargin()或setPadding()设置结果.在XML中,这可以通过将EditText包装到LinearLayo

android是否支持%或者是否有一种近似的方法。我有两个非常不同的屏幕大小,但就百分比而言,我希望活动中的EditBox在两个屏幕大小中具有相同的边距,作为屏幕大小的比例。如何做到这一点

它实际上不支持按百分比设置值(某些xml动画文件似乎除外),如果您死心塌地地使用百分比,我能想到的最好方法是从java调用getWidth和getHeight,然后将它们乘以小数,并使用setMargin()或setPadding()设置结果.

在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>

编辑:此答案中的布局现在已被弃用。使用建议的布局。另外,谢谢你的评论


支持库版本23.0.0(是时候了,对吧?)现在有了更好的方法。您现在可以使用或

如果您希望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>

ConstraintLayout
中引入了约束,这是可能的

例如,您可以将
EditText
top放置在屏幕高度的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>


如果您在
LinearLayout
中只有一个组件,它将不起作用。对于我来说,在LinearLayout中只有一个列表视图,并且无论是否在LinearLayout中添加
android:orientation=“horizontal”
,无论是否添加
minSdkVersion=16
。对于高度,您只需要布局高度=对于高度,您只需要为LinearLayout内的元素设置
layout\u height=“0dp”
(而不是布局宽度)。从理论上讲,您可以嵌套这两个元素来实现宽度和高度,尽管Android Studio会警告您嵌套权重不是最优的(但我总是忽略这一点),因为API级别26.1.0这应该是公认的答案。通过使用布局权重是可能的,但这是非常有限的,并且约束布局给了您更多的控制。这方面的最低API版本是什么?