Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ListView项中右对齐2文本视图_Android - Fatal编程技术网

Android 在ListView项中右对齐2文本视图

Android 在ListView项中右对齐2文本视图,android,Android,我有一个包含2个文本视图的列表视图。一个文本视图显示在气泡背景中 <?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="wrap_content" > <!--

我有一个包含2个文本视图的列表视图。一个文本视图显示在气泡背景中

<?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="wrap_content" >
    <!--    android:background="#FFDBE2ED"-->
 <LinearLayout 
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/wrapper2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
       >
    <!--  android:layout_gravity="center" -->
        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_marginTop="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginLeft="5dip"
            android:background="@drawable/bubble_yellow"
            android:paddingLeft="10dip"
            android:text="Hello bubbles!"
            android:textColor="@android:color/primary_text_light" />
         <TextView
            android:id="@+id/sender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_margin="0dip"
            android:background="#00dcdcdc"
            android:paddingLeft="10dip"
            android:text="Hello bubbles!"

           android:textColor="#b9dcdcdc"  
            android:layout_below="@+id/comment"
            />


    </RelativeLayout>

    </LinearLayout>


</LinearLayout>
当我在左侧显示2文本视图时,一切正常。但是,当我在右侧显示2 textview时,我希望2 textview是右对齐的。但到目前为止,我还不能做到这一点。

TextView.setGravity()
正在定义子视图的显示方式。您希望实际对齐textView,而不是其子项。您需要使用方法
setLayoutParams()
请参阅。我不确定你想用
包装器
布局视图做什么。因此,下面的代码可能不是您所需要的全部。但这是将视图与相对布局对齐的推荐方法(实用)

如果第二个视图需要与countryName精确对齐,则可以使用以下选项:

View yourOtherView = findViewById(R.id.yourOtherView);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_BELOW, R.id.comment);
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.comment);

yourOtherView.setLayoutParams(params);
这会告诉它在注释视图下方对齐,然后右对齐。您可能需要对边距、填充和其他值进行调整,以使其完全正确

祝你好运

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.listitem_discuss, parent, false);
    }

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

    OneMessage coment = getItem(position);

    countryName = (TextView) row.findViewById(R.id.comment);

    countryName.setText(coment.msgText);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    if(!coment.sentbyuser)
    {
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    }
    else
    {
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    }

    countryName.setLayoutParams(params);

    return row;
}
View yourOtherView = findViewById(R.id.yourOtherView);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_BELOW, R.id.comment);
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.comment);

yourOtherView.setLayoutParams(params);