视图中额外8dp保证金背后的原因?没有办法解决

时间:2015-07-03 16:36:45

标签: android android-layout android-xml

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

  • 在"查看"中获得8dp额外保证金的原因(在TextView下划线。)
  • 我在该视图中给出了48dp左边距。

在我的观点之上

<TextView> which has a drawable icon in left.
  • 左边距24dp和可绘制衬垫24dp。

做的原因。

  • 我尝试使用带黑色背景的视图在我的文字下创建下划线。
  • 我在xml中给出了48dp作为左边距。但如图所示,我得到56dp。

  • 行之间的差异是8dp。

screenshot of my app

enter image description here

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

这是我的活动文件。

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();
}
  • 谢谢你: - )

6 个答案:

答案 0 :(得分:2)

因为您设置了下划线视图&#39; layout_width =&#34; match_parent&#34;和layout_gravity =&#34; center&#34;。
观察后测量:
- 线性父视图的宽度为1080px;
- 下划线视图的宽度为936px(因为layout_marginLeft =&#34; 48dp&#34;(144px))
视图布局时:
- 因为线性父母的方向是&#34;垂直&#34;所以当设置layout_gravity =&#34; center&#34;等于layout_gravity =&#34; center_horizo​​ntal&#34;。
- 对于&#34; center_horizo​​ntal&#34;孩子,线性父母将使用它的X-center 来限制子视图的X-center 因此下划线视图的X轴将是(以px为单位):
540 (X-center of parent) + 144 (48dp margin left) - 468 (half of child's width) = 216px (72dp)
这就是为什么使用layout_gravity =&#34; center&#34;,你会看到下划线视图将获得24dp的额外余量。

答案 1 :(得分:0)

这是因为您的观看宽度为 match_parent &amp; android:layout_marginLeft&amp; android:layout_gravity="center" 导致视图移出屏幕范围。

See this for more information..

尝试从视图中删除中心layout_gravity(绘制线条)

android:layout_gravity="center"

并使用

android:layout_marginLeft="@dimen/space_large"

而不是

android:layout_marginLeft="@dimen/space_xxlarge"

答案 2 :(得分:0)

您正在向图标添加可绘制填充,并且下划线和图标之间的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>

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

答案 3 :(得分:0)

根据这两条线:

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,他们不会对齐。如果您希望元素以直观的方式显示,则需要更改其中一个。

答案 4 :(得分:0)

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

Missing icon width

你已经添加了24英尺的marginLeft以及24dp的drawablePadding,你说总共48dp但你忘记了可绘制图标的宽度。因此,ACCOUNT“A”的第一个后者不是48dp。

您正在使用LinearLayout,而且每个人都在左侧,因此不需要任何重力。

另外你说从View移除重力使它从左边36dp。对,那是正确的。您错过了计算中的可绘制图标宽度。

layout_margin的{​​{1}}设为等于View。这就是我想到的原因。

答案 5 :(得分:0)

只需删除

&#13;
&#13;
android:layout_gravity="center"
&#13;
&#13;
&#13;

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