【问题标题】:Aligning material edittexts, textviews, checkboxes, etc对齐材质edittext、textview、复选框等
【发布时间】:2016-06-28 03:26:36
【问题描述】:

我的问题是所有这些视图都有不同的左填充,当我将它们放在另一个下方时,它看起来很乱。

我该如何解决?有什么很酷的方法可以解决这个问题吗?

我尝试添加一些额外的填充,例如用于编辑文本的 2dp 和用于文本视图的 4dp,但由于不同设备上的填充不同,它似乎不起作用。

示例(在 Google 的官方应用程序中并非如此): 在我的布局中没有什么特别之处,只是没有任何附加属性的视图。

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

【问题讨论】:

  • 请分享您的布局。

标签: android


【解决方案1】:

没有很酷的方法可以做到这一点。以下策略最适合我:

【讨论】:

    【解决方案2】:

    您应该在包含表单的布局中放置一个填充,而不是为每个视图添加填充。这样,所有视图都将正确对齐。

    <LinearLayout
        ...
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">
    
        <!--Your form inputs-->
    
    </LinearLayout>
    

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

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

    【讨论】:

    • 是的,在我的活动/对话中就是这样。问题是每个内置的材质控制已经有不同的填充。
    【解决方案3】:

    我建议以这种方式管理边距和填充:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多