2013-04-05 2 views
0

Я искал это и не смог найти ответ, который работает на меня. На моей 5-й вкладке у меня есть 2 столбца. Я хотел бы сделать 1-й столбец 40% и 2-й столбец шириной 60%.Как изменить ширину столбцов в Android XML?

Я попытался использовать android: layout_weight и установил 1 объект в столбце 1 равным 0,4, а другой в столбце равным 0,6, но безрезультатно. Есть идеи?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TabHost 
     android:id="@+id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
      </TabWidget> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <LinearLayout 
        android:id="@+id/tab1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <Button android:text="Start" 
          android:id="@+id/btn_StartWatch" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"/> 

        <Button android:text="Stop" 
          android:id="@+id/btn_StopWatch" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"/> 

        <TextView android:text="TextView" 
           android:id="@+id/tv_ShowResults" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"/> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab2" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <TextView android:text="TextView2" 
           android:id="@+id/textView2" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"/> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab3" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <TextView android:text="TextView3" 
           android:id="@+id/textView3" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"/> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab4" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 
       </LinearLayout> 


       <LinearLayout 
        android:id="@+id/tab5" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <TableLayout android:id="@+id/details" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:stretchColumns="1" 
         android:paddingTop="4dp"> 
         <TableRow> 
          <TextView android:text="Name:" /> 
          <EditText 
           android:id="@+id/txtResName" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:ems="10" 
           android:inputType="textPersonName" > 
           <requestFocus /> 
          </EditText> 
         </TableRow> 

         <TableRow> 
          <TextView android:text="Type:" /> 
          <RadioGroup android:id="@+id/types"> 
           <RadioButton android:id="@+id/chcType1" 
            android:text="Type 1"/> 
           <RadioButton android:id="@+id/chcType2" 
            android:text="Type 2"/> 
          </RadioGroup> 

          <TextView android:text="Date:" /> 
          <EditText 
           android:id="@+id/txtResDate" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:ems="10" 
           android:inputType="date" /> 
         </TableRow> 

         <TableRow> 
          <TextView android:text="Time:" /> 
          <EditText 
           android:id="@+id/txtResTime" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:ems="10" 
           android:inputType="time" /> 

          <TextView android:text="Test" /> 
          <EditText 
           android:id="@+id/txtResNumber" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:ems="10" 
           android:inputType="number" /> 
         </TableRow> 

         <TableRow> 
          <TextView android:text="Test 2" /> 
          <EditText 
           android:id="@+id/txtResTest2" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:ems="10" 
           android:inputType="phone" /> 

          <TextView android:text="Notes:" /> 
          <EditText 
           android:id="@+id/txtResNotes" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:lines="2" 
           android:maxLines="2" 
           android:ems="10" 
           android:maxWidth="200sp" 
           android:inputType="textMultiLine" /> 
         </TableRow> 
        </TableLayout> 
       </LinearLayout> 
      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

ответ

1

Необходимо указать атрибут layout_width как «0px». Тогда ваш атрибут layout_weight будет определять фактическую ширину.

+0

Layout_width для tabhost? – tmparisi

+0

В этом случае да. Вам нужно установить либо layout_width, либо layout_height (но не оба) в 0px в любое время, когда вы хотите использовать макет. Когда вы это сделаете, атрибут, установленный в 0px (либо layout_width или layout_height), будет расширяться, чтобы заполнить остальную часть пространства. –

+0

Спасибо, это сработало с некоторыми изменениями. Вместо этого я добавил это в TableLayout. Made width = 0px. Затем установите вес = 100. Затем я устанавливаю каждый компонент в тот же TableRow, вес = 40 или 50 или любой другой. И установите каждый элемент width = 0px. Прекрасно работает! – tmparisi

Смежные вопросы