выравнивание по центру в RelativeLayout

#android #android-layout

#Android #android-макет

Вопрос:

введите описание изображения здесь

есть ли какой-нибудь способ центрировать текст под каждым изображением? Я жестко запрограммировал его для домашних заданий, но, как вы можете видеть, длина может варьироваться! Я использую относительный макет, а текстовое представление находится под каждым изображением?

Комментарии:

1. Взгляните на это samcoles.co.uk/mobile/android-implementing-a-dashboard-activity

Ответ №1:

Используя комбинацию линейных макетов, вы могли бы добиться чего-то подобного

 <LinearLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical"    
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="fill_parent" 
        android:layout_height="0dip"
        android:layout_weight="1.0" android:gravity="center_vertical">
        <LinearLayout android:layout_width="0dip" 
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="1.0" android:gravity="center">
            <ImageView android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:src="@drawable/icon"/>
            <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:text="Test" android:gravity="center"/>
        </LinearLayout>
        <LinearLayout android:layout_width="0dip" 
            android:layout_height="wrap_content"
            android:orientation="vertical" android:layout_weight="1.0" 
            android:gravity="center">
            <ImageView android:layout_width="wrap_content"                   
                android:layout_height="wrap_content"
                android:src="@drawable/icon"/>
            <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:text="Test" android:gravity="center"/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent" 
        android:layout_height="0dip"
        android:layout_weight="1.0" android:gravity="center_vertical">
        <LinearLayout android:layout_width="0dip" 
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="1.0" android:gravity="center">
            <ImageView android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:src="@drawable/icon"/>
            <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:text="Test" android:gravity="center"/>
        </LinearLayout>
        <LinearLayout android:layout_width="0dip" 
            android:layout_height="wrap_content"
            android:orientation="vertical" android:layout_weight="1.0" 
            android:gravity="center">
            <ImageView android:layout_width="wrap_content"                   
                android:layout_height="wrap_content"
                android:src="@drawable/icon"/>
            <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:text="Test" android:gravity="center"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
 

Но я настоятельно рекомендую проверить сообщение, которое я отправляю в качестве комментария. Он использует макет панели инструментов.

Ответ №2:

 <?xml version="1.0" encoding="utf-8"?>
 
 <ImageView android:id="@ id/imgLecture" android:src="@drawable/icon3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@ id/textView1" android:layout_alignParentLeft="true" android:layout_marginTop="30dp" android:layout_marginLeft="60dp"></ImageView>
<ImageView android:id="@ id/imgMail" android:src="@drawable/icon4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@ id/textView2" android:layout_alignParentRight="true" android:layout_marginTop="30dp" android:layout_marginRight="60dp"></ImageView>
  <TextView android:text="Lectures" android:id="@ id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_below="@ id/imgLecture" android:layout_alignParentLeft="true" android:layout_marginLeft="60dp" android:layout_marginTop="2dp" ></TextView>
<TextView android:text="Mail" android:id="@ id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_below="@ id/imgMail" android:layout_alignParentRight="true" android:layout_marginRight="60dp" android:layout_marginTop="2dp" ></TextView>

 <ImageView android:id="@ id/imgExam" android:src="@drawable/icon5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@ id/textView3" android:layout_alignParentLeft="true" android:layout_marginTop="30dp" android:layout_marginLeft="60dp"></ImageView>
<ImageView android:id="@ id/imgGrade" android:src="@drawable/icon6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@ id/textView4" android:layout_alignParentRight="true" android:layout_marginTop="30dp" android:layout_marginRight="60dp"></ImageView>
  <TextView android:text="Exams" android:id="@ id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_below="@ id/imgExam" android:layout_alignParentLeft="true" android:layout_marginLeft="60dp" android:layout_marginTop="2dp" ></TextView>
<TextView android:text="Grades" android:id="@ id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_below="@ id/imgGrade" android:layout_alignParentRight="true" android:layout_marginRight="60dp" android:layout_marginTop="2dp" ></TextView>
 

Ответ №3:

было очень сложно просмотреть ваш XML-файл … почему бы вам не использовать gridview, и для каждого представления вы можете иметь изображение и текст ниже … так намного проще…