简体   繁体   中英

Why i have in TextView margin?

I make simple xml file for android view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:id="@+id/recipe_step_number_textView"
        android:text="STEP #NUM"
        />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:id="@+id/recipe_step_title_editText"
        android:hint="add step title"/>

</LinearLayout>

He, how you see, have very simple TextView and EditText, but TextView have a margin a few pixels in Top, looks like this:

Studio屏幕

why i have margin top?

UPD Thank you very much! As I found out there are many ways to solve such trivial issues and almost all they fit

As per the default size of textview define here your are using wrap_content for it's height.. That's why it is showing something different as your edit text (Different margins)..
Either you have to give the text size for it...or you can change textview's height to match_parent .

Try like This

<?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="horizontal">

<TextView
    android:id="@+id/recipe_step_number_textView"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_gravity="top"
    android:gravity="center"
    android:text="STEP #NUM" />

<EditText
    android:id="@+id/recipe_step_title_editText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="4"
    android:hint="add step title" />

You give android:gravity="center" and give android:layout_weight="1" , here your text is large and in center. So, its looks like that.

Its better if you use RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/recipe_step_number_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="STEP #NUM" />

    <EditText
        android:id="@+id/recipe_step_title_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/recipe_step_number_textView"
        android:hint="add step title" />
</RelativeLayout>

or

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5">

    <TextView
        android:id="@+id/recipe_step_number_textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="STEP #NUM" />

    <EditText
        android:id="@+id/recipe_step_title_editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:hint="add step title" />
</LinearLayout>

Add weightSum to your Root Layout .

Refer this.

<?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="horizontal"
    android:weightSum="5">

    <TextView
        android:id="@+id/recipe_step_number_textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="STEP #NUM" />

    <EditText
        android:id="@+id/recipe_step_title_editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:hint="add step title" />

</LinearLayout>

EDIT 1 :

Add this line to your Text View if you don't want to give weightSum is the only Solution.

android:singleLine="true"

Or

EDIT 2:

Set specific size of Text View using.

android:textSize="12sp"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM