2012-02-15 4 views
0

enter image description hereAndroid: Таблица Расположение раскладки

Я хочу «Длительность» и «Стоимость» Выравнивание по правому краю. Я пробовал этот код, но он не работает.

<?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" > 

<com.markupartist.android.widget.ActionBar 
    android:id="@+id/actionBar" 
    style="@style/ActionBar" /> 

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:text="Date" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:text="Couse Name" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:layout_gravity="right" 
      android:text="Duration" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:paddingRight="5dip" 
      android:layout_gravity="right" 
      android:text="Cost" /> 
    </TableRow>  
</TableLayout>  
</LinearLayout> 

layout_gravity = "right" не работает. I trird gravity = "ritght", но это тоже не работает. Я также хочу, чтобы каждый столбец был выровнен по центру.

ответ

2

дают вес для строки таблицы и все виды текста ...

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TableRow 
     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:paddingLeft="5dip" 
      android:text="Date" android:layout_weight="1"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:text="Couse Name" android:layout_weight="1"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:paddingLeft="5dip" 
      android:text="Duration" android:layout_weight="1"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:paddingLeft="5dip" 
      android:paddingRight="5dip" 
      android:text="Cost" android:layout_weight="1"/> 
    </TableRow> 
</TableLayout> 
+0

Спасибо. :) – SkyWalker

+0

Да, конечно !! Могу ли я установить вес TextView из кода? – SkyWalker

+0

Вы можете установить его с помощью LayoutParams – Sniper

0

Для такого же макета лучше использовать RelativeLayout.

Зачем использовать RelativeLayout?

Просто потому, что мы можем установить взгляды, используя ниже атрибуты:

  1. андроида: layout_toRightOf
  2. андроид: layout_toLeftOf
  3. андроид: layout_alignParentRight
  4. андроид: layout_alignParentLeft
  5. андроида: layout_below

и многие другие атрибуты RelativeLayout обеспечивает установку представлений.

<RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:text="Date" 
      android:id="@+id/txtDate"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:text="Couse Name" 
      android:layout_toRightOf="@+id/txtDate" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:layout_gravity="right" 
      android:text="Duration" 
      android:layout_toLeftOf="@+id/txtCost" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:paddingRight="5dip" 
      android:layout_gravity="right" 
      android:text="Cost" 
      android:layout_alignParentRight="true" 
      android:id="@+id/txtCost" /> 

</RelativeLayout> 
+0

Спасибо, но я хочу, чтобы добавить динамические строки в эту таблицу из файла класса. Поэтому я не могу использовать Relative Layout. – SkyWalker

+0

@Hardik Полностью потеря времени .. ** Вы пишете где-нибудь в questio, что вы хотите программно отображать Views? ** –

+0

Нет, потому что моя первая проблема - установить макеты. – SkyWalker

0

Эй вы можете использовать относительный макет, так что вы можете разместить свои взгляды текст в любом месте вы хотите ...

ниже приведен пример кода:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" > 

    <TextView 
     android:id="@+id/usage" 
     android:layout_marginTop="220dip" 
     android:layout_marginLeft="45dip" 
     android:layout_marginRight="15dip" 
     android:typeface="serif"    
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" " 
     android:textColor="#030900"/> 

</RelativeLayout> 
+0

На самом деле я хочу добавить динамические строки в эту таблицу из файла класса. Поэтому я не могу использовать Relative Layout. – SkyWalker

1

Linear Layout нормально ... Проверьте это .. `

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="Date" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingLeft="5dip" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="Couse Name" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:paddingLeft="5dip" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="Duration" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:paddingLeft="5dip" 
      android:paddingRight="5dip" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="Cost" /> 
    </TableRow> 
</TableLayout> 

`

Изменить каждый layout_weight как вы хотите .. Надеюсь, что это помогает .. Удачи ...

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