Центрирование текстового изображения Ниже кнопки изображения, которая находится под относительной компоновкой?
У меня есть эта проблема, поскольку текст не центрируется под кнопкой изображения. в любом случае, чтобы исправить эту проблему? кодирование и изображение приведены ниже спасибо!
<LinearLayout android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnContact"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnRoute"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnChecList"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Ответы
Ответ 1
Попробуйте этот фрагмент:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center_vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="text" />
</RelativeLayout>
</LinearLayout>
![enter image description here]()
Ответ 2
Вы можете сделать, просто (!!!):
android:layout_alignRight="@id/my_image_view"
android:layout_alignLeft="@id/my_image_view"
просто выровняйте его влево и вправо вместе, в результате TextView будет центрирован.
Ответ 3
Просто добавьте эти значения:
<TextView
android:layout_alignLeft="@+id/my_image_view"
android:layout_alignRight="@+id/my_image_view"
android:gravity="center" />
Ответ 4
поместите кнопку и текст в ту же вертикаль LinearLayout
(и вы можете поместить LinearLayout
в относительный макет) и просто присвойте центр тяжести текстовому виду.
Ответ 5
android:layout_centerHorizontal="true"
добавьте это
Ответ 6
удалите строку android:layout_centerInParent="true"
из текстовых просмотров
или попробуйте это
<LinearLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
<ImageButton
android:contentDescription="@string/app_name"
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/ic_launcher" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:text="@string/app_name"
android:textColor="@color/black"/>
</LinearLayout>
Ответ 7
btw я пробовал делать это наоборот. я изменился с горизонтальной на вертикальную и работал
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget33"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF" >
<LinearLayout android:gravity="center" android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnCat1"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:clickable="false"
android:text="Cat 1" />
</RelativeLayout>
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnRoute"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:clickable="false"
android:text="Route" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:gravity="center" android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnContact"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:clickable="false"
android:text="Contacts" />
</RelativeLayout>
<RelativeLayout android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@null"
android:onClick="btnChecList"
android:src="@drawable/nopic" >
</ImageButton>
<TextView
style="@style/MenuStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:clickable="false"
android:text="CheckList" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>