2016-05-16 61 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> 
相关问题