Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

android - left and right alignment rows inside Listview?

My goal is below image

enter image description here

And i have below codes
row_right.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/me"
    android:layout_gravity="right"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="15dp"
    android:layout_gravity="right"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="15sp"
        android:textColor="#000000"
        android:text="TextView" />

</LinearLayout>



<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/dataAndTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="15sp"
            android:textColor="#001199"
            android:text="gggggggghyjkljgfdgjlkhfdhklggg" />

</LinearLayout>

</LinearLayout>

row_left.xml

<?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:background="@drawable/you"
    android:layout_gravity="left"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="15dp"
    android:layout_gravity="right"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="15sp"
        android:textColor="#000000"
        android:text="TextView" />

     </LinearLayout>



     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/dataAndTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="15sp"
            android:textColor="#001199"
            android:text="gggggggggg" />

      </LinearLayout>

</LinearLayout>

main.xml

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


    <ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="0px"
    android:layout_weight="1"
    android:scrollbars="vertical"
    android:divider="@null"
    android:dividerHeight="0dp" />

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

    <EditText
        android:id="@+id/content"
        android:layout_width="253dp"
        android:layout_height="wrap_content"
        android:autoText="false"
        android:capitalize="none"
        android:ems="10"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textSize="16sp"
        android:hint="Enter text"
        >       

    </EditText>

<Button
    android:id="@+id/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:text="Send" />

</LinearLayout>

</LinearLayout>

but my result become

enter image description here

How i can Fix it?

UPDATE1
my listAdapter.java

public class ListAdapter1 extends BaseAdapter {
private LayoutInflater myInflater;
private List<SmsInformation> list;

public ListAdapter1(Context context) {
    myInflater = LayoutInflater.from(context);

}

public void setData(List<SmsInformation> list2) {
    this.list = list2;

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;        

    if (convertView == null) {
        if (list.get(position).getTypeOfSms().equals("send"))
            convertView     = myInflater.inflate(R.layout.raw_left, null);
        else
            convertView     = myInflater.inflate(R.layout.raw_right, null);

        holder          = new ViewHolder();
        holder.message      = (TextView) convertView.findViewById(R.id.message);
        holder.dateAndTime      = (TextView) convertView.findViewById(R.id.dataAndTime);



        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.message.setText(list.get(position).getMessageContent());
    holder.dateAndTime.setText(list.get(position).geTime()+list.get(position).getDate());




    return convertView;
}

static class ViewHolder {
    TextView message;
    TextView dateAndTime;
}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Changes made:

~ in row_right.xml & row_left.xml, layout_width attribute of the parent LinearLayout should be set to wrap_content instead of match_parent

~ in main.xml, layout_width attribute of ListView should be set to match_parent. not wrap_content

Try the following code:

~ row_right.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/me"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:layout_gravity="right"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="15sp"
            android:textColor="#000000"
            android:text="TextView" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/dataAndTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="15sp"
            android:textColor="#001199"
            android:text="gggggggghyjkljgfdgjlkhfdhklggg" />

    </LinearLayout>

</LinearLayout>

</RelativeLayout>

~ row_left.xml

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

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/you"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:layout_gravity="right"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="15sp"
            android:textColor="#000000"
            android:text="TextView" />

     </LinearLayout>

     <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/dataAndTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="15sp"
            android:textColor="#001199"
            android:text="gggggggggg" />

      </LinearLayout>

</LinearLayout>

</RelativeLayout>

~ main.xml

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


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:scrollbars="vertical"
        android:divider="@null"
        android:dividerHeight="0dp" />

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

        <EditText
            android:id="@+id/content"
            android:layout_width="253dp"
            android:layout_height="wrap_content"
            android:autoText="false"
            android:capitalize="none"
            android:ems="10"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textSize="16sp"
            android:hint="Enter text"
        >       

        </EditText>

        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"        
            android:text="Send" />

    </LinearLayout>

</LinearLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...