S
S
Sergey Rolich2011-08-08 14:41:42
Android
Sergey Rolich, 2011-08-08 14:41:42

Help with layout design in Android

How can I make the button always be exactly half the screen, i.e. there are 4 of them as in the program "Who wants to be a millionaire." It is necessary that, depending on the content, they do not change their size, I managed to do it only if there is little text, they are arranged normally, if the text in one is larger in the other, one button seems to take a little space from the other.


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="@color/background"
    android:orientation="horizontal"
    >  
    
    
    <TableRow android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="1dp" android:layout_weight="0.45">  
        <TextView android:paddingTop="30dp" android:layout_height="wrap_content" android:id="@+id/question" style="@style/EffectTextViewQuestion" android:text="TextViewsdf" android:layout_width="wrap_content"></TextView>
    </TableRow>    

    <TableRow android:layout_width="fill_parent" android:layout_height="1dp" android:layout_weight="0.55" android:background="@color/background">
            <TableLayout android:id="@+id/tableLayout2" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_weight="1">
                <TableRow android:id="@+id/tableRow3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5">
                    <Button android:textColor="@color/text" android:layout_margin="10dp" android:text="Button" android:id="@+id/answer1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="0.1"></Button>
                    <Button android:textColor="@color/text" android:layout_margin="10dp" android:text="Button" android:id="@+id/answer2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="0.1"></Button>
                </TableRow>
                <TableRow android:id="@+id/tableRow4" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5">
                    <Button android:textColor="@color/text" android:layout_margin="10dp" android:text="Button" android:id="@+id/answer3" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="0.1"></Button>
                    <Button android:textColor="@color/text" android:layout_margin="10dp" android:text="Button" android:id="@+id/answer4" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="0.1"></Button>
                </TableRow>
            </TableLayout>
    </TableRow>  
 
</TableLayout>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Grichenko, 2011-08-08
@Kalobok

When specifying layout_weight, try playing around with the appropriate layout_width and layout_height. I don't remember the details, but either wrap_content or 0dp should be fine if I understand the problem correctly.
Well, at the same time, it's ugly to use match_parent and fill_parent side by side.

G
GSysoev, 2013-11-13
@GSysoev

For such purposes, RelativeLayout is just the thing. TableLayout is not a very convenient thing Here are 4 buttons:


<?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"><view android:id="@+id/emptyview" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /><button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_toleftof="@+id/emptyview" android:text="Button1" />

    

    <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_torightof="@+id/button1" android:text="Button2" />

    <button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignright="@+id/button1" android:layout_below="@+id/button1" android:text="Button3" />

    <button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/button2" android:layout_alignparentright="true" android:layout_below="@+id/button2" android:text="Button4" />

</relativelayout>

K
Konstantin, 2014-07-25
@Norraxx

Do you do HTML layout?
Why are you using TableLayout?

<LinearLayout
  orientation="vertical"
  weighSum="1">
  <View
    layout_width="wrap_content"
    layout_height="0dp"
    layout_weight=".5" />
  <TextView
    layout_gravity="center" />
  <View
    layout_width="wrap_content"
    layout_height="0dp"
    layout_weight=".5" />
</LinearLayout>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question