0

This is my current layout.

enter image description here

And my current layout code

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >

<TextView
    android:id="@+id/stockView"
    android:text="TextView"
    android:layout_weight="1" />

<Button
    android:id="@+id/stockQuote"
    style="?android:attr/buttonStyleSmall"
    android:text="@string/get_stock_quote" 
      android:layout_weight="1" />

<Button
    android:id="@+id/webStock"
    style="?android:attr/buttonStyleSmall"
    android:text="@string/go_to_website"
      android:layout_weight="1"  />

</TableRow>

Everything works out fine, as I reasoned that because I gave each element a layout_width of "1", they take up the same amount of space in the table row. However I want the text view to take up more room than the button so i changed the text view's layout_weight to 2 so that it can take up half of the extra space while each button takes up 1/4 of the button. After I made this change, this is what I got for my layout. enter image description here

Does anyone understand why this happened? The text view isn't even visible now. How can I fix this to have text view take up a bit more room?

nem035
  • 34,790
  • 6
  • 87
  • 99
committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • Do you mean "layout_*weight*" when you say "layout_*width*"? – aij Sep 25 '14 at 20:13
  • you can set the `weightSum` of the `TableRow` to be `4`, then use the values `(2, 1, 1)` you wanted. You can also use floating point values for `layout_weight` for more exact size relationships. – nem035 Sep 25 '14 at 20:14
  • What was the weight sum before? Is this only with TableRows ? – committedandroider Sep 25 '14 at 21:38

3 Answers3

1

weightSum Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children.

<TableRow android:weightSum="1">
<TextView
    android:id="@+id/stockView"
    android:text="TextView"
    android:layout_weight="0.5" />

<Button
    android:id="@+id/stockQuote"
    style="?android:attr/buttonStyleSmall"
    android:text="@string/get_stock_quote" 
      android:layout_weight="0.25" />

<Button
    android:id="@+id/webStock"
    style="?android:attr/buttonStyleSmall"
    android:text="@string/go_to_website"
      android:layout_weight="0.25"  />
</TableRow>

you can read more about it here

Community
  • 1
  • 1
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
1

weightSum = TOTAL ( 100% )

layout_weight = %

Try this:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" 
android:weightSum="100">

<TextView
    android:id="@+id/stockView"
    android:text="TextView"
    android:layout_weight="40" />

<Button
    android:id="@+id/stockQuote"
    style="?android:attr/buttonStyleSmall"
    android:text="@string/get_stock_quote" 
      android:layout_weight="30" />

<Button
    android:id="@+id/webStock"
    style="?android:attr/buttonStyleSmall"
    android:text="@string/go_to_website"
      android:layout_weight="30"  />

</TableRow>
extmkv
  • 1,991
  • 1
  • 18
  • 36
0

You are about to give the total partition ratio you need to do

Ex: If you wanna split the width of the screen to 3 then the weightSum should be 3 and the split ratio you should give to every view individually

Below are the sample code please try this,

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:weightSum="1" >

    <TextView
        android:id="@+id/stockView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_weight="0.5" />

    <Button
        android:id="@+id/stockQuote"
        android:layout_width="0dp"
        android:layout_height="wrap_content"            
        style="?android:attr/buttonStyleSmall"
        android:text="@string/get_stock_quote" 
        android:layout_weight="0.25" />

    <Button
        android:id="@+id/webStock"
        android:layout_width="0dp"
        android:layout_height="wrap_content"            
        style="?android:attr/buttonStyleSmall"
        android:text="@string/go_to_website"
        android:layout_weight="0.25"  />

</TableRow>
Bethan
  • 971
  • 8
  • 23