2016-06-27 65 views
0

我的问题是,所有这些视图有不同的左边距,当我把它们放在另一个下面时,它看起来很混乱。对齐材质edittexts,textviews,复选框等

我该如何解决?有没有很酷的方法来解决这个问题?

我试图把一些额外的填充,如2dp为edittexts和4dp为textviews,但它似乎不工作,因为不同的设备上填充不同。

示例(在Google官方应用中并非如此): enter image description here 这里没有什么特别的布局,只是没有任何附加属性的视图。

<?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="wrap_content" 
    android:orientation="vertical"> 

    <android.support.design.widget.TextInputLayout 
     android:id="@+id/input_layout_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="0dip"> 

     <EditText 
      android:id="@+id/text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/network_password" 
      android:inputType="textPassword"/> 

    </android.support.design.widget.TextInputLayout> 

    <CheckBox 
     android:id="@+id/checkbox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/show_password"/> 

</LinearLayout> 
+0

请分享您的布局。 –

回答

1

与其为每个视图添加填充,您应该将其中一个添加到包含该窗体的布局。这样,所有视图都将被正确对齐。

<LinearLayout 
    ... 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="16dp"> 

    <!--Your form inputs--> 

</LinearLayout> 

新创建的活动在根布局周围已经有16dp的填充。

值'16dp'应该保存为维数,而不是硬编码。

+0

是的,多数民众赞成在我的活动/对话。问题是每一个内置的材质控制已经有了不同的填充。 – JaLoveAst1k

0

我会建议管理margin和padding这样:

  1. /res/根据屏幕密度(如values-w820dp)然后创建dimens.xml创建多个values文件夹在每个屏幕密度的适当值的文件夹下。 follow this guide
  2. 如果可能,始终使用RelativeLayout而不是LinearLayout。如果您的问题只是对齐,那么您总是可以使用RelativeLayout的属性(如android:layout_alignRight="@id/id_of_view")来管理对齐。