视图中额外 8dp 边距背后的原因?不是解决方法

Reason behind extra 8dp margin in a view ? not ways to solve

在我的 activity xml 文件中,我在视图的左侧获得了额外的 8dp 边距(表示为下划线)。

在那个视图之上我有

<TextView> which has a drawable icon in left.

这样做的原因。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FAFAFA"
android:orientation="vertical"
tools:context="com.hysterics.delhishop.AccountSetting">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:layout_marginTop="16dp"
            android:gravity="center|left"
            android:paddingLeft="16dp"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:text="@string/hello_user"
            android:textColor="@color/primary_text"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/user_account_information"
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:layout_marginLeft="24dp"
            android:drawableLeft="@drawable/ic_account_box_black_18dp"
            android:drawablePadding="24dp"
            android:gravity="center|left"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:text="@string/account_information"
            android:textColor="@color/primary_text"
            android:textSize="15sp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="center"
            android:layout_marginLeft="48dp"
            android:background="@android:color/darker_gray"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:layout_marginLeft="24dp"
            android:drawableLeft="@drawable/ic_home_black_18dp"
            android:drawablePadding="24dp"
            android:gravity="center|left"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:text="@string/account_address"
            android:textColor="@color/primary_text"
            android:textSize="15sp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="center"
            android:layout_marginLeft="48dp"
            android:background="@android:color/darker_gray"/>
................
................

    </LinearLayout>

</ScrollView>

这是我的 activity 文件。

public class AccountSetting extends AppCompatActivity {

public static final String TAG_USER_NAME_DIALOG = "edit_text_dialog";

@InjectView(R.id.account_setting_toolbar) Toolbar accountSettingToolbar;
@InjectView(R.id.user_account_information) TextView userAccountInformation;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account_setting);
    ButterKnife.inject(this);
    setToolbar();
}

因为你的视图宽度是 match_parent & android:layout_marginLeft & android:layout_gravity="center" 导致视图移出屏幕边界。

尝试从您的视图(画线)中删除中心 layout_gravity

android:layout_gravity="center"

并使用

android:layout_marginLeft="@dimen/space_large"

而不是

android:layout_marginLeft="@dimen/space_xxlarge"

您正在为图标添加可绘制的内边距,下划线和图标的 marginLeft 完全不同。而且您还必须考虑图标本身的大小。我会惊讶于它会有完全相同的对齐方式。

而不是这个,你为什么不在两个线性布局之间使用带有权重的水平 LinearLayout,一个带有图标,另一个透明视图与下划线具有相同的高度,另一个线性布局包含文本和下划线完美对齐。没有利润没有什么,只是重量分配。像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:weightSum="10">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Account Information"
            android:textSize="20sp" />

        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

这只是一种可以保证精确对齐的可能解决方案。

根据这两行:

android:drawableLeft="@drawable/ic_account_box_black_18dp"
android:drawablePadding="@dimen/space_large"

您将有一个宽度为 18dp 的可绘制对象,然后是一个 @dimen/space_large 的填充,看起来是 24dp,总共有 42dp 个填充TextView 的左边缘和文本本身的开始。

但是,您的行中的 layout_marginLeft@dimen/space_xxlarge48dp。因为一个是 42dp 而另一个是 48dp,它们不会对齐。如果您希望元素在视觉上显示成一行,则需要更改其中一个。

<View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="center"
            android:layout_marginLeft="48dp"
            android:background="@android:color/darker_gray"/>

您的布局中有多个视图,您正在使用 layout_gravity="center"。所以 View 试图将自己调整到 LinearLayout 的中心。只需尝试删除 layout_gravity 或使用 layout_gravity="left".

你加了24dp的marginLeft和24dp的drawablePadding你说一共48dp但是你忘记了drawable icon的宽度。因此,ACCOUNT "A" 的第一个后者不在 48dp 边距处。

您正在使用 LinearLayout 并且一切都在左侧,因此不需要任何重力。

您还说从 View 中移除引力会使它从左侧移动 36dp。对,那是正确的。您在计算中错过了可绘制图标的宽度。

Viewlayout_margin 设置为 48dp + width of icon。我想这就是你得到的原因。

因为你设置下划线视图的layout_width="match_parent"和layout_gravity="center".
观看后测量:
- 线性 parent 视图的宽度为 1080px;
- 下划线视图的宽度为 936px(因为 layout_marginLeft="48dp"(144px))
当查看布局时:
- 因为线性parent的方向是"vertical",所以当设置layout_gravity="center"等于layout_gravity="center_horizontal".
- 对于 "center_horizontal" child, 线性 parent 将 child 视图的 X-center 与它的 X-center
所以下划线视图的 X-axis 将是(以像素为单位):
540 (X-center of parent) + 144 (48dp margin left) - 468 (half of child's width) = 216px (72dp)
这就是为什么使用 layout_gravity="center",你会看到下划线视图将获得 24dp 的额外边距。

只需删除

android:layout_gravity="center"

从您的角度来看,它将解决您的问题。