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

北战南征 提交于 2019-12-12 13:01:59

问题


In my activity xml file i am get an extra 8dp margin in Left side in view(Represented as Underline).

  • Reason for getting 8dp margin extra in "view"? (underline under TextView.)
  • i have given 48dp left margin in that view.

above that view i have

<TextView> which has a drawable icon in left.
  • with left margin 24dp and drawable padding 24dp.

Reason for doing.

  • I am try to create an underline under my words using a view with black background.
  • i have given 48dp as left margin in xml.but as shown in photo i am getting 56dp.

  • difference between lines is 8dp.

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

here is my activity file.

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();
}
  • thank you in adavnce :-)

回答1:


Because you set the underline view's layout_width="match_parent" and layout_gravity="center".
After views measure:
- the linear parent view's width is 1080px;
- the underline view's width is 936px ( because of layout_marginLeft="48dp"(144px))
When views layout:
- Because the linear parent's orientation is "vertical", so when set layout_gravity="center" equal with layout_gravity="center_horizontal".
- For a "center_horizontal" child, the linear parent will margin the child view's X-center with it's X-center
So the X-axis of underline view will be (in px):
540 (X-center of parent) + 144 (48dp margin left) - 468 (half of child's width) = 216px (72dp)
That why with layout_gravity="center", you will see the underline view will get 24dp extra margin.




回答2:


You are adding drawable padding to the icon and the marginLeft is completely different between the underline and the icon. And also you have to take into account the size of the icon itself. I would be surprised that it would have the exact same align.

Instead of this, why don't you use an horizontal LinearLayout with weight between two linear layouts, one with the icon and another transparent view with the same height as the underline, and the other linear layout that contains text and underline perfectly aligned. No margins no nothing, just distribution of weight. Something like this:

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

This is just one possible solution that can guarantee you exact alignments.




回答3:


It because your views width is match_parent & android:layout_marginLeft & android:layout_gravity="center" is causing view to shift outside of screen bound.

See this for more information..

Try with removing center layout_gravity from your Views(which draws line)

android:layout_gravity="center"

and use

android:layout_marginLeft="@dimen/space_large"

instead of

android:layout_marginLeft="@dimen/space_xxlarge"



回答4:


According these two lines:

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

You'll have a drawable of width 18dp, then a padding of @dimen/space_large, which looks to be 24dp for a total of 42dp of padding between the left edge of the TextView and the start of the text itself.

However, the layout_marginLeft on your lines is @dimen/space_xxlarge or 48dp. As one is 42dp and the other is 48dp, they won't align. You'll need to change one or the other if you want the elements to appear visually in line.




回答5:


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

There are multiple view in your layout and you are using layout_gravity="center". So View is trying to adjust itself to center inside LinearLayout. Just try removing layout_gravity or use layout_gravity="left".

You have added marginLeft of 24dp also drawablePadding of 24dp and you say a total of 48dp But you forget the width of drawable icon. So, the first latter of ACCOUNT "A" is not at 48dp margin.

You are using LinearLayout and everyting is at the left side so there is no need of any gravity.

Also you say removing gravity from View makes it 36dp from left. YES, thats correct. You missed Drawable Icon width from your calculation.

Set layout_margin of View equals 48dp + width of icon. Thats the reason you got I think.




回答6:


Just remove

android:layout_gravity="center"

from your view it will fix your problem.



来源:https://stackoverflow.com/questions/31211152/reason-behind-extra-8dp-margin-in-a-view-not-ways-to-solve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!