I have the following layout i'd like to make the textview appear in the center and middle of the view. How can i acheive this?
我有以下布局我想让textview出现在视图的中心和中间。我该如何实现这一目标?
I've tried adjusting the various gravity attributes when looking at the layout in graphical view, but nothing seems to change it. I'd like the textview in the center which i've done but halfway down the view.
我在图形视图中查看布局时尝试调整各种重力属性,但似乎没有任何改变。我想在中心的textview我已经完成但是在视图的中间。
thanks matt.
<?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="match_parent"
android:orientation="vertical"
android:background="@drawable/carefreebgscaledalphajpg" >
<TextView
android:id="@+id/textviewdatefornocallspage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="You have no calls for "
android:textSize="25sp" />
</LinearLayout>
10 个解决方案
#1
41
set the gravity to center in your linearLayout tag :
将重力设置为linearLayout标记的中心:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/carefreebgscaledalphajpg" >
or use a RelativeLayout
like this :
或使用像这样的RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/carefreebgscaledalphajpg" >
<TextView
android:id="@+id/textviewdatefornocallspage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="You have no calls for "
android:textSize="25sp" />
</RelativeLayout>
#2
6
Set android:gravity="center"
in linear layout and keep the text view's height and width as wrap_content
.
在线性布局中设置android:gravity =“center”,并将文本视图的高度和宽度保持为wrap_content。
This Solution worked for me.
这个解决方案对我有用。
#3
3
Try this
simple i have tried to many answer but all answers are not working ......finally this methods works for me
简单,我试过很多回答,但所有的答案都没有起作用......最后这种方法适合我
<LinearLayout
android:layout_below="@+id/iv_divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="@dimen/_10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mycredits"
android:textColor="@color/colorGray"
android:gravity="center"
android:textSize="@dimen/_25dp" />
</LinearLayout>
#4
0
Add android:gravity="center_horizontal"
to your LinearLayout and alter your TextView's layout_width as android:layout_width="wrap_content"
将android:gravity =“center_horizontal”添加到LinearLayout并将TextView的layout_width更改为android:layout_width =“wrap_content”
<?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="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/carefreebgscaledalphajpg" >
<TextView
android:id="@+id/textviewdatefornocallspage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="You have no calls for "
android:textSize="25sp" />
</LinearLayout>
#5
0
set width of your textview to wrap_content
of your textview and set your LinearLayout to android:gravity=center_horizontal
将textview的宽度设置为textview的wrap_content,并将LinearLayout设置为android:gravity = center_horizontal
#6
0
Sounds like you want android:layout_gravity
for the TextView
听起来你想要android:layout_gravity for TextView
see docs: developer.android.com
请参阅docs:developer.android.com
#7
0
<TextView
...
android:layout_gravity="center"
....
/>
#8
0
If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center"
. For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center". If you want to use more than one view in this layout, you should use RelativeLayout and set android:layout_centerInParent="true"
.
如果在linearlayout你的方向垂直,你可以通过android:layout_gravity =“center”将textview放在“水平中心”。要垂直居中textview,您需要将textview的layout_height设置为match_parent并将android:gravity设置为“center”。如果要在此布局中使用多个视图,则应使用RelativeLayout并设置android:layout_centerInParent =“true”。
#9
0
Use Relative Layout, it always gives more accurate and flexible layout
使用相对布局,它总是提供更准确和灵活的布局
<?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="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
#10
0
set android:gravity="center"
in your Linear Layout
在线性布局中设置android:gravity =“center”
#1
41
set the gravity to center in your linearLayout tag :
将重力设置为linearLayout标记的中心:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/carefreebgscaledalphajpg" >
or use a RelativeLayout
like this :
或使用像这样的RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/carefreebgscaledalphajpg" >
<TextView
android:id="@+id/textviewdatefornocallspage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="You have no calls for "
android:textSize="25sp" />
</RelativeLayout>
#2
6
Set android:gravity="center"
in linear layout and keep the text view's height and width as wrap_content
.
在线性布局中设置android:gravity =“center”,并将文本视图的高度和宽度保持为wrap_content。
This Solution worked for me.
这个解决方案对我有用。
#3
3
Try this
simple i have tried to many answer but all answers are not working ......finally this methods works for me
简单,我试过很多回答,但所有的答案都没有起作用......最后这种方法适合我
<LinearLayout
android:layout_below="@+id/iv_divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="@dimen/_10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mycredits"
android:textColor="@color/colorGray"
android:gravity="center"
android:textSize="@dimen/_25dp" />
</LinearLayout>
#4
0
Add android:gravity="center_horizontal"
to your LinearLayout and alter your TextView's layout_width as android:layout_width="wrap_content"
将android:gravity =“center_horizontal”添加到LinearLayout并将TextView的layout_width更改为android:layout_width =“wrap_content”
<?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="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/carefreebgscaledalphajpg" >
<TextView
android:id="@+id/textviewdatefornocallspage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="You have no calls for "
android:textSize="25sp" />
</LinearLayout>
#5
0
set width of your textview to wrap_content
of your textview and set your LinearLayout to android:gravity=center_horizontal
将textview的宽度设置为textview的wrap_content,并将LinearLayout设置为android:gravity = center_horizontal
#6
0
Sounds like you want android:layout_gravity
for the TextView
听起来你想要android:layout_gravity for TextView
see docs: developer.android.com
请参阅docs:developer.android.com
#7
0
<TextView
...
android:layout_gravity="center"
....
/>
#8
0
If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center"
. For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center". If you want to use more than one view in this layout, you should use RelativeLayout and set android:layout_centerInParent="true"
.
如果在linearlayout你的方向垂直,你可以通过android:layout_gravity =“center”将textview放在“水平中心”。要垂直居中textview,您需要将textview的layout_height设置为match_parent并将android:gravity设置为“center”。如果要在此布局中使用多个视图,则应使用RelativeLayout并设置android:layout_centerInParent =“true”。
#9
0
Use Relative Layout, it always gives more accurate and flexible layout
使用相对布局,它总是提供更准确和灵活的布局
<?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="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
#10
0
set android:gravity="center"
in your Linear Layout
在线性布局中设置android:gravity =“center”