2014-01-24 71 views
0

我想在我的文本视图居中一些代码,但它似乎是持续左对齐:TextView的文本不居中

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerVertical="true" 
    android:layout_above="@+id/myButtons">  

<!-- Text --> 
<TextView 
android:id="@+id/myText" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:textSize="22sp" 
android:text="Test" 
    android:textStyle="bold" /> 

<!-- And the thumbnail --> 
<ImageView 
android:id="@+id/image" 
android:layout_marginTop="30dp" 
android:layout_marginBottom="30dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:contentDescription="@string/image_text" 
    android:layout_gravity="center" />  
</LinearLayout> 

图像(当有一个,有时它被设置为可见。去)是中心对齐好吗。这只是我无法让文字居中对齐。

任何想法的欢迎。谢谢!

+0

你有没有后试过我的答案? – Nambi

+0

是的,我回答了你的回答:) – b85411

+0

我编辑了我的答案看看 – Nambi

回答

0

在你的TextView属性添加android:gravity="center_horizontal|center_vertical",使中心

<TextView 
    android:id="@+id/myText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal|center_vertical" 
    android:textSize="22sp" 
    android:text="Test" 
    android:textStyle="bold" /> 

使TextView的fillParent ImageView的走了

TextView的FILL_PARENT的设定高度通过编程像

textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,0)); 
             ^      ^
              height      width      
+0

感谢user2675669,这使得它水平居中对齐,这比我得到更多,所以谢谢你!但只有一件事,在图像视图被隐藏(Visibility.GONE)的情况下,如何让文本视图占据所有可用空间?因为现在,而不是垂直居中对齐,它是顶部对齐(大概是因为它设置为包装内容,而图像填充父项?)。任何想法赞赏,谢谢! – b85411