Android 如何使用合并列创建2列布局?

Android 如何使用合并列创建2列布局?,android,gridview,tableview,tablelayout,Android,Gridview,Tableview,Tablelayout,如何在android中创建这种布局 我试过使用表格布局。但是在创建这个时会出现错误。 请检查我的代码,我可以显示图像。一行中有一个图像。但我想在一行2个小图像。并继续 这是我的密码 private void getBannerList() { final Call<BannerList_Model> itemMainResponse = APIHandler.getApiService().bannerList(getFiledMapForItemList());

如何在android中创建这种布局 我试过使用表格布局。但是在创建这个时会出现错误。 请检查我的代码,我可以显示图像。一行中有一个图像。但我想在一行2个小图像。并继续

这是我的密码

private void getBannerList() {

    final Call<BannerList_Model> itemMainResponse = APIHandler.getApiService().bannerList(getFiledMapForItemList());

    itemMainResponse.enqueue(new Callback<BannerList_Model>() {
        @Override
        public void onResponse(Response<BannerList_Model> response, Retrofit retrofit) {
            BannerList_Model itemResponse = null;
            itemResponse = response.body();

            if (itemResponse == null) {
                UIUtils.showToast(getContext(), "Server Error !");
            } else {
                String msg = itemResponse.getMsg().toString();
                if (msg.equals("success")) {
                    userList = itemResponse.getBannerList();
                    if(userList != null) {
                        TableLayout table = (TableLayout) view.findViewById(R.id.tblBanner_list);
                        table.setColumnStretchable(0,true);
                        table.setColumnStretchable(1,true);

                        int tsize = userList.size();
                        TextView[] t1 = new TextView[tsize];
                        ImageView[] img = new ImageView[tsize];

                        for(int i=0; i<userList.size();i++)
                        {
                            TableRow row = new TableRow(getContext());
                            String banner_size = userList.get(i).getBannerSize().toString();


                                img[i] = new ImageView(getContext());
                                Picasso.with(getContext())
                                        .load(Constants.VIDEO_URL + userList.get(i).getBannerUrl())
                                        .into(img[i]);

                                row.addView(img[i]);

                                table.addView(row);

                        }
                    }
                } 
            }
        }
        }
    });
}
private void getBannerList(){
最后一个调用itemMainResponse=APIHandler.getApiService().bannerList(getFiledMapForItemList());
itemMainResponse.enqueue(新回调(){
@凌驾
公共响应(响应、改装){
BannerList_Model itemResponse=null;
itemResponse=response.body();
if(itemResponse==null){
showtoos(getContext(),“服务器错误!”);
}否则{
字符串msg=itemResponse.getMsg().toString();
如果(消息等于(“成功”)){
userList=itemResponse.getBannerList();
if(userList!=null){
TableLayout table=(TableLayout)view.findviewbyd(R.id.tblBanner\u列表);
table.setColumnStretchable(0,true);
table.setColumnStretchable(1,true);
int tsize=userList.size();
TextView[]t1=新的TextView[tsize];
ImageView[]img=新的ImageView[tsize];
对于(int i=0;i
您的错误是将
TextView
添加到
TableRow
(行),但未将行添加到
TableLayout
。下面是一个示例,说明您需要什么。您可以设置一些边距并添加按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>

</TableLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="TextView" />

    </TableRow>

</TableLayout>

</LinearLayout>


是的,但我想动态创建。会有项目列表。你能为第2行和第3行的textview使用layout_span=2属性吗?你能给我不带xml的代码吗?我需要通过编程来完成吗?你的用户列表大小是4吗?没有N个列表。如果有2个小图像,那么它将在2列中,如果大图像超过1列。你会怎么做u检查是否有2个小图像或1个大图像?您的图像在哪里?使用json。我已通过横幅大小,小,完整。图像将从服务器加载。使用api调用。您的图像大小控制在哪里?没有代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>

</TableLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="TextView" />

    </TableRow>

</TableLayout>

</LinearLayout>