2014-03-05 104 views
1

我很新的Android開發,並會喜歡一些幫助創建Android上的一個簡單的佈局。這個佈局將用於列表視圖。創建Android的佈局

這是我要的樣子: good layout 這是我所得到的(不要笑): bad layout

文本底部行重疊的頂部,這個數字應該是右側(圖像的左側)和不同行中的圖像應該對齊。

(是的,我知道這個顏色是醜陋的。)

這裏是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/thelayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="6dip" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <RelativeLayout 
     android:id="@+id/widget375" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="5"> 
      <TextView 
       android:id="@+id/txt2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="14pt" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/txt1" /> 
      <TextView 
       android:id="@+id/txt3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/txt1" 
       android:layout_alignParentLeft="true" /> 
      <TextView 
       android:id="@+id/txt1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="14pt" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" /> 
    </RelativeLayout> 
    <ImageView 
     android:id="@+id/theicon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout> 

謝謝!

+1

張貼您的佈局xml。 –

+0

什麼都嘗試過。張貼 –

+1

發表您的xml文件... – rajshree

回答

1

對於text1,請使用android:layout_alignParentRight而不是android:layout_alignParentLeft

對於text3,添加android:layout_below="@+id/txt1"

嘗試如下....

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/thelayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="6dip" > 

    <RelativeLayout 
     android:id="@+id/widget375" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" > 

     <TextView 
      android:id="@+id/txt3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/txt1" /> 

     <TextView 
      android:id="@+id/txt1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:textSize="14pt" /> 

     <TextView 
      android:id="@+id/txt2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:textSize="14pt" /> 
    </RelativeLayout> 

    <ImageView 
     android:id="@+id/theicon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

</LinearLayout> 
+0

謝謝,這個作品很棒! – vkapadia

1

試試這個代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/thelayout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:padding="6dip" > 

<LinearLayout 
    android:id="@+id/widget375" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/txt1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
      android:text="qwerty" 
      android:textSize="14pt" /> 

     <TextView 
      android:id="@+id/txt2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="right|center_vertical" 
      android:text="123" 
      android:textSize="14pt" /> 
    </LinearLayout> 

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

<ImageView 
    android:id="@+id/theicon" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" /> 

+0

很棒!我選擇了另一個答案,因爲它更有效率(6個對象而不是7個)。謝謝您的幫助! – vkapadia

0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/thelayout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="6dip" > 

<RelativeLayout 
    android:id="@+id/widget375" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/txt2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/txt1" 
     android:layout_toLeftOf="@+id/theicon" 
     android:textSize="14pt" /> 

    <TextView 
     android:id="@+id/txt3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txt1" 
     android:layout_alignLeft="@+id/txt1" 
     android:layout_alignRight="@+id/txt2" 
     /> 

    <TextView 
     android:id="@+id/txt1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:textSize="14pt" /> 

    <ImageView 
     android:id="@+id/theicon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     /> 
</RelativeLayout> 

</LinearLayout> 
+0

這種佈局仍然把數只是名稱的 – vkapadia

4
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" > 

       <TextView 
        android:id="@+id/textView2" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="abc" 
        android:textSize="14pt" /> 

       <TextView 
        android:id="@+id/textView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"      
        android:textSize="14pt" 
        android:text="abc" /> 
      </LinearLayout> 

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

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:gravity="center" > 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 
+0

PLS接受的答案,如果它是你 – Anu

+0

真的很有幫助如果答案是正確的,那麼用戶必須接受你的答案正確 –

+0

對不起,我睡覺去了:)我今天早上我正在嘗試所有這些答案,在我檢查完畢後我會選擇一個答案。 – vkapadia

0

試試這個..

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/thelayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="6dip" > 

    <LinearLayout 
     android:id="@+id/widget375" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <TextView 
       android:id="@+id/txt1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.7" 
       android:textSize="14pt" 
       android:singleLine="true"/> 

      <TextView 
       android:id="@+id/txt2" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.3" 
       android:singleLine="true" 
       android:textSize="14pt" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/txt3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:singleLine="true"/> 
    </LinearLayout> 

    <ImageView 
     android:id="@+id/theicon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"/> 

</LinearLayout> 
+0

這個數字還有點偏左。它應該只在圖像的左側 – vkapadia

0

你可以試試下面的代碼視圖要。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="60dp" 
> 
<TextView android:id="@+id/item1" 
     android:text="textview1" 
     android:layout_marginTop="10dp" 
     android:layout_height="wrap_content" 
     android:layout_width="70dp" 
     android:width="20dip" 
    /> 
    <TextView android:id="@+id/item2" 
     android:text="textview2" 
     android:layout_marginTop="5dp" 
     android:layout_below="@+id/item1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:width="100dip" 
    /> 
    <TextView android:id="@+id/item3" 
     android:text="textview3" 
      android:layout_marginTop="10dp" 
     android:layout_marginLeft="90dp" 
     android:layout_toRightOf="@+id/item1" 
     android:layout_height="wrap_content" 
     android:layout_width="70dp" 
     android:width="20dip" 
    /> 

    <ImageView android:id="@+id/Imageview" 
     android:background="@drawable/ic_launcher" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_height="40dp" 
     android:layout_width="40dp" 
     android:width="20dip"/> 
</RelativeLayout> 

它會幫助你。

謝謝