Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout issue - relative widths in percent using weight

Tags:

android

layout

I am trying to assign relative widths to columns in a ListView that is in a TabHost, using layout_weight as suggested here:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
        <FrameLayout android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">
           <TableLayout  
              android:id="@+id/triplist" 
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:paddingTop="4px">
              <TableRow>
                 <ListView android:id="@+id/triplistview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>
              </TableRow>
              <TableRow>
                 <Button android:id="@+id/newtripbutton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Add Trip"/>
              </TableRow>
[other tabs ...]

My row definition has 4 columns that I would like to size as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:weightSum="1.0"
   android:padding="4px">   
   <TextView android:id="@+id/rowtripdate"
      android:layout_weight=".2"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:inputType="date"/>
   <TextView android:id="@+id/rowodostart"
      android:layout_weight=".2"
      android:layout_width="0dip"
      android:layout_height="wrap_content"/>
   <TextView android:id="@+id/rowodoend"
      android:layout_weight=".2"
      android:layout_width="0dip"
      android:layout_height="wrap_content"/>
   <TextView android:id="@+id/rowcomment"
      android:layout_weight=".4"
      android:layout_width="0dip"
      android:layout_height="wrap_content">

Unfortunately, it seems to want to fit all the columns into the space that the button occupies, as opposed to the width of the screen. Or maybe there is another constraint that I do not understand. I'd appreciate your help.

alt text
(source: heeroz.com)

like image 675
cdonner Avatar asked May 26 '10 16:05

cdonner


People also ask

How is weight used in linear layout?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

Which type of layout allows the components in relative position in android?

Android Layout Code This Relative Layout consists of a Vertical LinearLayout within a Nested Horizontal LinearLayout along with a Child RelativeLayout.

Can we use weight in RelativeLayout?

Last year, android has introduced a new layout called PercentRelativeLayout which gives us the functionality of both relativelayout and weight property together .

How do you use relative layout?

Positioning Views. RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.


1 Answers

I think I forgot this property for the TableLayout element:

android:stretchColumns="0"

It appears to be working now.

like image 56
cdonner Avatar answered Nov 04 '22 12:11

cdonner