2014-03-05 102 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> 

它会帮助你。

谢谢