Разделите ширину между элементами таблицы равномерно - PullRequest
2 голосов
/ 21 октября 2010

Я пытаюсь создать таблицу для отображения что-то вроде этого:

 ________
|__|__|__|
|__|__|__|

Все поля должны быть разделены равномерно, и в среднем поле в нижнем ряду будет очень большое изображение, которое будет уменьшено. Это мой текущий макет XML, но я получаю такой макет:

 ________
|_|____|_|
|_|____|_|

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

    <TableRow>
        <TextView
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="Unused"
            android:padding="3dip" />
        <TextView
            android:layout_column="2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="Top Center"
            android:padding="3dip" />
        <TextView
            android:layout_column="3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="Unused"
            android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="----&gt;" />
        <ImageView
            android:id="@+id/mainImg"
            android:layout_column="2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
        />
        <TextView
            android:layout_column="3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="&lt;----" />
    </TableRow>
</TableLayout>

Что я делаю не так?

Ответы [ 4 ]

5 голосов
/ 21 октября 2010

Вы должны использовать линейные макеты с весами.

Попробуйте, возможно, вам придется добавить еще кое-что, но это должно помочь вам начать.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
            <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
            <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        </LinearLayout>    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
            <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
            <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
</LinearLayout>
1 голос
/ 21 октября 2010

Я не уверен, что ты делаешь неправильно.Я пробовал макет стола, как у вас в прошлом, и не мог заставить его работать должным образом.Я обнаружил, что выполнение макета, подобного вашему, было проще с использованием относительного макета.

Вы устанавливаете левое поле в left_align, правое поле в right_align и т. Д. Затем присвойте им все значение падения, чтобы соответствоватьразмер экрана, который вы используете в данный момент, и он будет корректно обновляться на экранах других размеров.

К сожалению, как только вы выйдете за пределы 3 (скажем, 4 или 5 блоков), это исправление больше не работает.

Редактировать: Согласно комментарию Мэла, альтернативой является использование относительной компоновки с его кодом XML.

Я опубликую свое исходное исправление для справки.Я также не упомянул, что это на самом деле относительный макет внутри макета фрейма.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >  

    <Button
            android:id="@+id/start"
            android:layout_width="110dip"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
        android:text="@string/title_start" />

    <Button
        android:id="@+id/options"
        android:layout_width="110dip"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="@string/title_options" />   

    <Button
        android:id="@+id/scores"
        android:layout_width="110dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="@string/title_scores" />  

</RelativeLayout>

0 голосов
/ 06 ноября 2015

Обновление: Библиотека поддержки процентов Android помогает нам равномерно делить любые виджеты Android, используя атрибут процента.

Код и концепция

Github Demo

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/fifty_huntv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff7acfff"
        android:text="20% - 50%"
        android:textColor="@android:color/white"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@id/fifty_huntv"
        android:background="#ffff5566"
        android:text="80%-50%"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="50%"
        />

</android.support.percent.PercentRelativeLayout>

Действительно потрясающе.

0 голосов
/ 01 декабря 2012

Попробуйте android: layout_weight = "1" в вашей кнопке / дочерних элементах RowLayout

enter image description here

     <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str1"  
            android:layout_weight="1"/>

        <Button
            android:id="@+id/button10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str2"  
            android:layout_weight="1"/>

        <Button
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str3"  
            android:layout_weight="1"/>

        <Button
            android:id="@+id/button12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/strMinus"  
            android:layout_weight="1"/>

    </TableRow>
...