[英]Centering a TextView vertically in a LinearLayout
我正在尝试在LinearLayout中将TextView居中,并且将其水平居中而不是垂直居中。
下面是我的代码
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/taque"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:text="Question"
android:textColor="@color/Black"
android:textSize="16dp"
android:typeface="normal" />
</LinearLayout>
尝试这个!
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/taque"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:text="Question"
android:textColor="@color/Black"
android:textSize="16dp"
android:typeface="normal" />
</LinearLayout>
您需要的只是TextView的android:gravity="center"
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/taque"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:text="Question"
android:textColor="@color/Black"
android:textSize="16dp"
android:typeface="normal" />
</LinearLayout>
除了textview的宽度和高度,您的布局看起来还不错。 只需将textview的宽度和高度从match_parent更改为wrap_content
<TextView
android:id="@+id/taque"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
只需将android:gravity=“center“
到您的TextView和LinearLayout中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.