左,里面的列表视图右对齐行? [英] left and right alignment rows inside Listview?

查看:264
本文介绍了左,里面的列表视图右对齐行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是下图

和我有以下codeS
row_right.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@可绘制/我
    机器人:layout_gravity =右
    机器人:layout_marginLeft =5DP
    机器人:layout_marginRight =10dp
    机器人:layout_marginBottom =5DP
    机器人:layout_marginTop =5DP
    机器人:方向=垂直>


<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =0dp
    机器人:layout_weight =5
    机器人:layout_marginLeft =5DP
    机器人:layout_marginRight =15dp
    机器人:layout_gravity =右
    机器人:layout_marginTop =5DP
    机器人:layout_marginBottom =5DP
    机器人:方向=横向>

    <的TextView
        机器人:ID =@ + ID /消息
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=右
        机器人:TEXTSIZE =15sp
        机器人:文字颜色=#000000
        机器人:文本=TextView的/>

< / LinearLayout中>



<的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1
        机器人:layout_marginBottom =5DP
        机器人:layout_marginLeft =5DP
        机器人:layout_marginRight =15dp
        机器人:方向=横向>

        <的TextView
            机器人:ID =@ + ID / dataAndTime
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=右
            机器人:TEXTSIZE =15sp
            机器人:文字颜色=#001199
            机器人:文本=gggggggghyjkljgfdgjlkhfdhklggg/>

< / LinearLayout中>

< / LinearLayout中>
 

row_left.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@可绘制/你
    机器人:layout_gravity =左
    机器人:layout_marginLeft =10dp
    机器人:layout_marginRight =5DP
    机器人:layout_marginBottom =5DP
    机器人:layout_marginTop =5DP
    机器人:方向=垂直>

<的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =0dp
    机器人:layout_weight =5
    机器人:layout_marginLeft =5DP
    机器人:layout_marginRight =15dp
    机器人:layout_gravity =右
    机器人:layout_marginTop =5DP
    机器人:layout_marginBottom =5DP
    机器人:方向=横向>

    <的TextView
        机器人:ID =@ + ID /消息
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=右
        机器人:TEXTSIZE =15sp
        机器人:文字颜色=#000000
        机器人:文本=TextView的/>

     < / LinearLayout中>



     <的LinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =0dp
        机器人:layout_weight =1
        机器人:layout_marginBottom =5DP
        机器人:layout_marginLeft =5DP
        机器人:layout_marginRight =15dp
        机器人:方向=横向>

        <的TextView
            机器人:ID =@ + ID / dataAndTime
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=右
            机器人:TEXTSIZE =15sp
            机器人:文字颜色=#001199
            机器人:文本=GGGGGGGGGG/>

      < / LinearLayout中>

< / LinearLayout中>
 

main.xml中

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:后台=#FFFFFF>


    <的ListView
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =0px
    机器人:layout_weight =1
    机器人:滚动条=垂直
    机器人:分隔=@空
    机器人:dividerHeight =0dp/>

 <的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向>

    <的EditText
        机器人:ID =@ + ID /内容
        机器人:layout_width =253dp
        机器人:layout_height =WRAP_CONTENT
        机器人:自动图文集=假
        机器人:利用=无
        机器人:EMS =10
        机器人:scrollHorizo​​ntally =真
        机器人:单线=真
        机器人:TEXTSIZE =16SP
        机器人:提示=输入文本
        >

    < /的EditText>

<按钮
    机器人:ID =@ + ID /发送
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文=发送/>

< / LinearLayout中>

< / LinearLayout中>
 

但我的成绩成为

我怎样才能解决这个问题?

UPDATE1
我listAdapter.java

 公共类ListAdapter1扩展了BaseAdapter {
私人LayoutInflater myInflater;
私人列表< SmsInformation>清单;

公共ListAdapter1(上下文的背景下){
    myInflater = LayoutInflater.from(上下文);

}

公共无效使用setData(名单< SmsInformation> list2中){
    this.list = list2中;

}

@覆盖
公众诠释getCount将(){
    返回则为list.size();
}

@覆盖
公共对象的getItem(INT位置){
    返回null;
}

@覆盖
众长getItemId(INT位置){
    返回0;
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    ViewHolder持有人;

    如果(convertView == NULL){
        如果(list.get(位置).getTypeOfSms()。等于(发送))
            convertView = myInflater.inflate(R.layout.raw_left,NULL);
        其他
            convertView = myInflater.inflate(R.layout.raw_right,NULL);

        持有人=新ViewHolder();
        holder.message =(TextView中)convertView.findViewById(R.id.message);
        holder.dateAndTime =(TextView中)convertView.findViewById(R.id.dataAndTime);



        convertView.setTag(保持器);
    } 其他 {
        支架=(ViewHolder)convertView.getTag();
    }

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




    返回convertView;
}

静态类ViewHolder {
    TextView的消息;
    DateAndTime的TextView的;
}

}
 

解决方案

作出更改:

〜在 row_right.xml &放大器; row_left.xml layout_width 父属性的LinearLayout 应设置代替 match_parent

WRAP_CONTENT

〜在的ListView 的main.xml layout_width 属性应设置为 match_parent 。没有 WRAP_CONTENT

请尝试以下code:

〜row_right.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

<的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@可绘制/我
    机器人:layout_alignParentRight =真
    机器人:layout_marginLeft =5DP
    机器人:layout_marginRight =10dp
    机器人:layout_marginBottom =5DP
    机器人:layout_marginTop =5DP
    机器人:方向=垂直>

    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =0dp
        机器人:layout_weight =5
        机器人:layout_marginLeft =5DP
        机器人:layout_marginRight =15dp
        机器人:layout_gravity =右
        机器人:layout_marginTop =5DP
        机器人:layout_marginBottom =5DP
        机器人:方向=横向>

        <的TextView
            机器人:ID =@ + ID /消息
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=右
            机器人:TEXTSIZE =15sp
            机器人:文字颜色=#000000
            机器人:文本=TextView的/>

    < / LinearLayout中>

    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =0dp
        机器人:layout_weight =1
        机器人:layout_marginBottom =5DP
        机器人:layout_marginLeft =5DP
        机器人:layout_marginRight =15dp
        机器人:方向=横向>

        <的TextView
            机器人:ID =@ + ID / dataAndTime
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=右
            机器人:TEXTSIZE =15sp
            机器人:文字颜色=#001199
            机器人:文本=gggggggghyjkljgfdgjlkhfdhklggg/>

    < / LinearLayout中>

< / LinearLayout中>

< / RelativeLayout的>
 

〜row_left.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

<的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@可绘制/你
    机器人:layout_alignParentLeft =真
    机器人:layout_marginLeft =10dp
    机器人:layout_marginRight =5DP
    机器人:layout_marginBottom =5DP
    机器人:layout_marginTop =5DP
    机器人:方向=垂直>

    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =0dp
        机器人:layout_weight =5
        机器人:layout_marginLeft =5DP
        机器人:layout_marginRight =15dp
        机器人:layout_gravity =右
        机器人:layout_marginTop =5DP
        机器人:layout_marginBottom =5DP
        机器人:方向=横向>

        <的TextView
            机器人:ID =@ + ID /消息
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=右
            机器人:TEXTSIZE =15sp
            机器人:文字颜色=#000000
            机器人:文本=TextView的/>

     < / LinearLayout中>

     <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =0dp
        机器人:layout_weight =1
        机器人:layout_marginBottom =5DP
        机器人:layout_marginLeft =5DP
        机器人:layout_marginRight =15dp
        机器人:方向=横向>

        <的TextView
            机器人:ID =@ + ID / dataAndTime
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=右
            机器人:TEXTSIZE =15sp
            机器人:文字颜色=#001199
            机器人:文本=GGGGGGGGGG/>

      < / LinearLayout中>

< / LinearLayout中>

< / RelativeLayout的>
 

〜main.xml中

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:后台=#FFFFFF>


    <的ListView
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =0px
        机器人:layout_weight =1
        机器人:滚动条=垂直
        机器人:分隔=@空
        机器人:dividerHeight =0dp/>

    <的LinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=横向>

        <的EditText
            机器人:ID =@ + ID /内容
            机器人:layout_width =253dp
            机器人:layout_height =WRAP_CONTENT
            机器人:自动图文集=假
            机器人:利用=无
            机器人:EMS =10
            机器人:scrollHorizo​​ntally =真
            机器人:单线=真
            机器人:TEXTSIZE =16SP
            机器人:提示=输入文本
        >

        < /的EditText>

        <按钮
            机器人:ID =@ + ID /发送
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文=发送/>

    < / LinearLayout中>

< / LinearLayout中>
 

My goal is below image

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

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;
}

}

解决方案

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>

这篇关于左,里面的列表视图右对齐行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆