简体   繁体   English

表格布局textview对齐

[英]table layout textview alignment

In my application I am displaying a table layout, in which I aligned 4 textviews in a row. 在我的应用程序中,我正在显示一个表格布局,其中我将4个textview连续对齐。

My table layout is: (I don't know how much good design is this) 我的表格布局是:(我不知道这是多少个好的设计)

<TableLayout 
    android:id="@+id/tablelayout"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:paddingRight="2dip"
    android:stretchColumns="4">
    <TableRow >
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Income" />
        <TextView
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Expense" 
            android:layout_marginLeft="-150dp" />
    </TableRow>    
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <View
            android:id="@+id/line1"
            android:layout_width="wrap_content"
            android:layout_height="1dip"
            android:layout_weight="1"
            android:background="#FF909090"
            android:padding="2dip" />
    </TableRow>
    <TableRow 
        android:id="@+id/tablerowhouse"   
        android:layout_marginTop="30px">
        <TextView  
            android:id="@+id/text40" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" />            
        <TextView             
            android:id="@+id/houseinctot" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" 
            android:layout_marginLeft="-250dp" />   
        <TextView 
            android:id="@+id/text41" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" 
            android:layout_marginLeft="-150dp" />          
        <TextView
            android:id="@+id/houseexptot" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" 
            android:layout_marginLeft="-70dp" 
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <View
            android:id="@+id/line1"
            android:layout_width="wrap_content"
            android:layout_height="1dip"
            android:layout_weight="1"
            android:background="#FF909090"
            android:padding="2dip" />
    </TableRow>
    <TableRow 
        android:layout_marginTop="30px">
        <TextView
            android:id="@+id/text42" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel:" 
            android:layout_span="3" />    
        <TextView
            android:id="@+id/trlinctot"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel" 
            android:layout_marginLeft="-250dp" 
            android:layout_alignParentRight="true" /> 
        <TextView  android:id="@+id/text43" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel:" 
            android:layout_marginLeft="-150dp" />           
        <TextView             
            android:id="@+id/trlexptot" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel:" 
            android:layout_marginLeft="-70dp"  
            android:gravity="right" />    
    </TableRow>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <View
            android:id="@+id/line1"
            android:layout_width="wrap_content"
            android:layout_height="1dip"
            android:layout_weight="1"
            android:background="#FF909090"
            android:padding="2dip" />
    </TableRow>


     <TableRow  android:layout_marginTop="30px">
 <TextView  android:id="@+id/text44" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_span="4">
     </TextView>     
      <TextView
           android:id="@+id/eduinctot"
           android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education" android:layout_marginLeft="-250dp" android:layout_alignParentRight="true">
     </TextView>  

           <TextView  android:id="@+id/text45" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_marginLeft="-150dp" >
     </TextView>            
        <TextView             
            android:id="@+id/eduexptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_marginLeft="-70dp"  android:gravity="right"
            >
     </TextView>    
 </TableRow>

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

            <View
                android:id="@+id/line1"
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_weight="1"
                android:background="#FF909090"
                android:padding="2dip" />
        </TableRow>

        <TableRow  android:layout_marginTop="30px">
 <TextView  android:id="@+id/text46" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group4:" android:layout_span="4">
     </TextView>     
      <TextView
           android:id="@+id/grp4inctot"
           android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group4" android:layout_marginLeft="-250dp">
     </TextView>  

           <TextView  android:id="@+id/text47" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group4:" android:layout_marginLeft="-150dp" >
     </TextView>            
        <TextView             
            android:id="@+id/grp4exptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group4:" android:layout_marginLeft="-70dp"  android:gravity="right"
            >
     </TextView>    
 </TableRow> 

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

            <View
                android:id="@+id/line1"
                android:layout_width="wrap_content"
                android:layout_height="1dip"
                android:layout_weight="1"
                android:background="#FF909090"
                android:padding="2dip" />
        </TableRow>

             <TableRow  android:layout_marginTop="30px">
 <TextView  android:id="@+id/text48" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group5:" android:layout_span="4">
     </TextView>     
      <TextView
           android:id="@+id/grp5inctot"
           android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group5" android:layout_marginLeft="-250dp">
     </TextView>  

           <TextView  android:id="@+id/text49" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group5:" android:layout_marginLeft="-150dp" >
     </TextView>            
        <TextView             
            android:id="@+id/grp5exptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group5:" android:layout_marginLeft="-70dp"  android:gravity="right"
            >
     </TextView>    
 </TableRow> 

Now what i want is: 现在我想要的是:

textview1:value(textview2)     textview3:value(textview4)
textview5:value(textview6)     textview7:value(textview8) .

I should right allign textview2 and textview6 (with in half of the screen) and should right align textview6 and textview8 我应该对所有的textview2和textview6(在屏幕的一半处)正确对齐,并应将textview6和textview8正确对齐。

 What i have:                         What i want:
    12345   4565                         12345    4565
    12        56                            12      56
    56        44                            56      44  
    6          6                             6       6

您在所有Textview中编写android:gravity="right"属性。

You should use a relative layout where you have ALIGN_PARENT_RIGHT option. 您应该使用具有ALIGN_PARENT_RIGHT选项的relative layout

Something like this 像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingTop="8dip"
    android:paddingBottom="8dip"
>

<TextView  
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="turn on" 
/>

<CheckBox 
    android:id="@+id/chekcbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    /> 
</RelativeLayout>

android:layout_alignParentRight="true" will place your textview to extreme right. android:layout_alignParentRight="true"将您的textview放在最右边。 Just try once it worked for me. 只要对我有用,就尝试一下。

Try this layout.This works. 尝试这种布局。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="123456" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="1234" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="123456" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_label1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="16" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="1" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="1" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM