2016-05-16 47 views
2

我爲Android設備上查看簡單的XML文件:爲什麼我在TextView保證金?

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

他,你怎麼看,有很簡單的TextView和EditText上,但是TextView的有餘量排名前幾個像素,看起來是這樣的:

Screen from Studio

爲什麼我有保證金最高?

UPD非常感謝!正如我發現有很多方法來解決這些小問題,幾乎所有他們適合

+1

此邊距因重力而變,因爲您使用的是android:gravity =「center」,所以textview的所有文本都在textview的中心偏移,因此您在文本頂部看到邊距 –

回答

3

按照TextView中的默認大小定義here您正在使用WRAP_CONTENT爲它的高度.. 這就是爲什麼它顯示更好的東西不同於你的編輯文本(不同的邊距)..
要麼你必須給文本大小它...或者你可以更改textview的高度match_parent

+0

thx @Saurabh!你的回答對我有幫助。我將textview的高度設置爲match_parent,但對layout_gravity =「center」的解決方案在我看來更加通用。而thx爲你的鏈接,這對我的學習很有幫助。 – abbath0767

+0

我的快樂親愛的...你也可以保持它的重力屬性..快樂編碼:) –

0

weightSum添加到您的Root Layout

請參閱此。

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

編輯1:

此行添加到您的Text View如果你不想給weightSum是唯一的解決方案。

android:singleLine="true" 

或者

編輯2:Text View使用

設置特定大小。

android:textSize="12sp" 
+1

沒有任何意義! –

+0

@NiravRanpara檢查這個的輸出。根本不需要Downvote。根本不顯示保證金。 –

+0

thx爲@jaydroider回答,但這不是我的問題的解決方案(我讓你怎麼說,但據我瞭解[文檔](http://developer.android.com/intl/ru/reference/android/widget /LinearLayout.html#attr_android:weightSum)此參數不是必需的) – abbath0767

2

嘗試像這樣

<?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的答案!我確實喜歡你的建議。但我做這個:android:layout_gravity =「center」 – abbath0767

1

你給android:gravity="center",給android:layout_weight="1",在這裏你的文字大,在中心。所以,它看起來像那樣。

它,如果你使用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> 

<?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> 
相關問題