Android 动态创建的适配器textview未正确对齐

Android 动态创建的适配器textview未正确对齐,android,dynamic,uiview,textview,Android,Dynamic,Uiview,Textview,在我的android应用程序中,我正在动态创建textview,每个textview都有一个与webservice发送的类型相关的onclick 最后一个文本必须与下一行对齐,下面我已经添加了布局细节 我的适配器类 ArrayList<GroupTitleVo> titlelist = activitylist.get(position) .getTitlelist(); LinearLayout sample_layout = ne

在我的android应用程序中,我正在动态创建textview,每个textview都有一个与webservice发送的类型相关的onclick

最后一个文本必须与下一行对齐,下面我已经添加了布局细节

我的适配器类

ArrayList<GroupTitleVo> titlelist = activitylist.get(position)
                .getTitlelist();
        LinearLayout sample_layout = new LinearLayout(_context);
        sample_layout.setLayoutParams(new LinearLayout.LayoutParams(
                0, 0));
        sample_layout.setOrientation(LinearLayout.HORIZONTAL);
        for (int i = 0; i < titlelist.size(); i++) {

            if (titlelist.get(i).getType().equals("user")) {
                TextView user_text = new TextView(_context);
                user_text.setId(i);
                user_text.setTextColor(Color.parseColor("#000000"));
                user_text.setTextSize(12);
                user_text.setTypeface(null, Typeface.BOLD);
                user_text.setText(" "+titlelist.get(i).getName());
                user_text.setTag(titlelist.get(i).getId()+"~"+titlelist.get(i).getName());

                user_text.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String id = (String) v.getTag();
                        Toast.makeText(_context, "user id" + id,
                                Toast.LENGTH_SHORT).show();
                        String[] name = id.split("~");
                        listener.userProfileredirect(name[0],name[1]);
                    }
                });

                holder.horizontaltext.addView(user_text);
            } else if (titlelist.get(i).getType().equals("verb")) {
                TextView verb_text = new TextView(_context);
                verb_text.setId(i);
                verb_text.setText(" "+titlelist.get(i).getName());
                verb_text.setTextSize(10);
                verb_text.setTag(titlelist.get(i).getId());
                holder.horizontaltext.addView(verb_text);
            } else {
                TextView group_text = new TextView(_context);
                group_text.setId(i);
                group_text.setTextColor(Color.parseColor("#000000"));
                group_text.setTextSize(14);
                group_text.setTypeface(null, Typeface.BOLD);
                group_text.setTag(titlelist.get(i).getId() + "~"
                        + titlelist.get(i).getType());
                group_text.setTextSize(12);
                group_text.setText(" "+titlelist.get(i).getName());

                group_text.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String id = (String) v.getTag();
                        Toast.makeText(_context, "Group ID" + id,
                                Toast.LENGTH_SHORT).show();
                        String idtype[] = id.split("~");
                        if (idtype.length > 1) {
                            listener.userGroupRedirect(idtype[1], idtype[0]);
                        }
                    }
                });

                holder.horizontaltext.addView(group_text);
            }

        }
ArrayList titlelist=activitylist.get(位置)
.getTitlelist();
LinearLayout示例\u布局=新的LinearLayout(\u上下文);
示例_layout.setLayoutParams(新的LinearLayout.LayoutParams(
0, 0));
样本_布局。设置方向(线性布局。水平);
对于(int i=0;i1){
userGroupRedirect(idtype[1],idtype[0]);
}
}
});
holder.horizontaltext.addView(组文本);
}
}
和XML父布局

  <LinearLayout
                android:id="@+id/textlinearlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/posted_person_img_view_id"
                android:orientation="horizontal" >
            </LinearLayout>


请帮助我对齐此文本视图

您需要制作嵌套布局

线性布局

您必须设置水平
LinearLayout
,以便将所有项目都放在一个水平行中。您需要在现有的
LinearLayout
图像视图中添加
ImageView
,并在其中创建另一个
LinearLayout
垂直设置,以使两个
TextView
项目一个接一个

您的视图应该如下所示:

<?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="match_parent"
              android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_gallery"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="First text"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Second text text"/>

    </LinearLayout>
</LinearLayout>
我在这里使用了XML文件,因为我认为可以更清楚地看到需要使用哪些属性和视图

编辑:要创建
RelativeLayout
程序,请阅读:


希望能有所帮助。

在水平
线性布局中添加
视图
只会将
视图
粘贴到最后。您可以在当前设置中使用嵌套的垂直
线性布局
,向其中添加
视图
,也可以对项目的根
视图
使用
相对布局
,并在新的
视图
RelativeLayout.LayoutParams
上设置
RelativeLayout.END_的
RelativeLayout.down
规则。我想动态创建的可能是textview将增加到20,在这种情况下,我如何使用xmlMadhu,看看你的代码。您已经有了带有三个元素的
LinearLayout
。我认为最简单的方法是将线性变为相对,并在视图中添加位置参数,如
textView1.toTheRightOf(textView2)
。我从来没有像你一样使用过布局,我总是定义一个.xml布局文件,然后编写一个代码。感谢这个链接,它提供了动态创建textview的想法,就像mike一样suggest@Madhu如果你还有问题,这个问题仍然悬而未决,你还没有接受我的答案作为解决办法:-)我认为你的答案不正确,只有你的链接帮我了解了一些情况,我怎么能接受你的答案
<?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"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_gallery"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="First text"
        android:layout_above="@+id/textView2"
        android:layout_toRightOf="@+id/image"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Second text text"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/image"/>

</RelativeLayout>