Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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 如何为所有视图提供恒定的左边距?_Android_Views - Fatal编程技术网

Android 如何为所有视图提供恒定的左边距?

Android 如何为所有视图提供恒定的左边距?,android,views,Android,Views,我有多个水平线性布局。在垂直列出的每个布局中,我都有一个TextView,后跟一个EditText。我想给所有的EditText视图留一个固定的左边距,这样它们才能对齐,我怎么才能做到呢?以下是部分代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="

我有多个水平线性布局。在垂直列出的每个布局中,我都有一个TextView,后跟一个EditText。我想给所有的EditText视图留一个固定的左边距,这样它们才能对齐,我怎么才能做到呢?以下是部分代码:

<?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:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/pNameL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="Segoe ui"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            style="@style/editText_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >
        </EditText>

    </LinearLayout>

在res/values/style.xml文件中创建样式。如果style.xml不存在,则创建一个新的。

<style name="editText_style">
    <item name="android:layout_marginLeft">30dp</item>
    <item name="android:layout_marginRight">30dp</item>
</style>

编辑:如果父对象是水平方向的,则不能相对于父对象应用边距。

尝试使用
布局:权重
属性。例如,如果希望EditText从其父视图的一半开始,并且父视图中包含EditText的视图总数为2,则将两个视图的
layout:weight
设置为0.5。这使两个视图在其父视图中具有相等的空间

仅当要进行更改的所有
线性布局
包含相同数量的视图时,此操作才有效。如果对于不同的
LinearLayout
包含不同数量的视图,则不能使用样式,或者无法从单个位置进行更改。


注意:使用
layout:weight
属性无法获取静态边距值。它取决于您的父级大小,并且可能因不同的屏幕大小而有所不同。

是的,您是对的,但问题是我想为所有编辑文本提供一个与父级布局相关的恒定边距,但是,这样做会在EditText之前为TextView提供一个左边距wtr。@user2254838边距始终相对于其父视图。你可能做错了什么。请发布您的代码,以便我可以查看。我刚刚编辑了我的初始帖子,给出了部分代码。现在,TextView之后的EditText获得了30dp的边距,但是wtrTextView@user2254838好啊在这里,EditText的父对象具有水平方向。在这种情况下,无法应用相对于父级的保证金。您可以使用
layout:weight
属性。请看我的编辑。我想,使用relativeLayout可能会是更好的选择,但我现在想这样做。。。我想我必须继续解决这个问题
style="@style/editText_style"