2016-05-16 8 views
2

Ich mache einfache XML-Datei für Android Ansicht:Warum habe ich in TextView Rand?

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

Er, wie Sie sehen, haben sehr einfache Textview und EditText, aber Textview haben einen Spielraum ein paar Pixel in Top, sieht wie folgt aus:

Screen from Studio

Warum habe ich Rand oben?

UPD Vielen Dank! Wie ich es gibt viele Möglichkeiten herausgefunden, so triviale Probleme zu lösen und fast alle passen sie

+1

Dieser Rand wird von der Schwerkraft, weil Sie Android verwenden: Schwerkraft = "Zentrum", so dass der Text Textansicht in der Mitte von Textansicht verschieben, so dass Sie den Rand oben im Text sehen –

Antwort

3

Gemäß der Standardgröße von Textview definieren here Ihre wrap_content verwenden dafür Höhe ist .. dass, warum es etwas zeigt anders als Ihr Text bearbeiten (Verschiedene Ränder) ..
Entweder Sie müssen die Textgröße für geben es ... oder Sie können textview Höhe zu ändern.

+0

thx @Saurabh! Ihre Antwort ist hilfreich für mich. Ich mache eine textview Höhe auf match_parent, aber Lösung mit layout_gravity = "center" schien mir vielseitiger. Und thx für Ihren Link, es ist nützlich für mein Lernen. – abbath0767

+0

Mine Vergnügen Liebe ... Sie können auch Schwerkraft Attribut bleiben..Happy Coding :) –

0

weightSum zu Ihrem Root Layout hinzufügen.

Siehe dies.

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

Fügen Sie diese Zeile in Text View wenn Sie wollen nicht geben weightSum die einzige Lösung ist.

android:singleLine="true" 

Oder

EDIT 2:

Set spezifische Größe von Text View verwenden.

android:textSize="12sp" 
+1

macht keinen Sinn! –

+0

@NiravRanpara Überprüfen Sie die Ausgabe von diesem. Keine Notwendigkeit für Downvote überhaupt. Es wird überhaupt keine Marge angezeigt. –

+0

thx für die Antwort @jaydroider, aber das ist keine Lösung für mein Problem (ich mache wie Sie sagen, aber wie ich die [Dokumentation] verstehe (http://developer.android.com/intl/ru/reference/android/widget /LinearLayout.html#attr_android:weightSum) dieser Parameter ist nicht obligatorisch) – abbath0767

2

wie das versuchen

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

+0

Thx für die Antwort! Ich habe dich gerne beraten. aber ich mache das: android: layout_gravity = "center" – abbath0767

1

Sie android:gravity="center" geben und android:layout_weight="1" geben, hier Ihren Text groß und in der Mitte ist. So sieht es aus.

Es ist besser, wenn Sie verwenden 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> 

oder

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