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

时间:2018-01-16 10:38:24

标签: android gridview tableview tablelayout

如何在android中创建这种布局 我尝试过使用表格布局。但在创建这个时遇到错误。 请检查我的代码,我能够显示图像。 1行中的1张图片。但我想要一排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);

                        }
                    }
                } 
            }
        }
        }
    });
}

这是我想要布局的视频 http://shrivivah.com/layout.mp4

如果有2个小图像,则显示在1行中。 如果有2个中等或全尺寸图像,则每个显示1行。 如果每行显示1个小图像和2个中图像,则显示。

布局图片

enter image description here

2 个答案:

答案 0 :(得分:0)

您的错误是您要将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>

答案 1 :(得分:-1)

 <?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"
    tools:context="com.example.tops.stackoverflow.MainActivity">

<LinearLayout
    android:id="@+id/llShortcutParent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:duplicateParentState="true"
    android:elevation="20dp"
    android:padding="10dp"
    android:orientation="horizontal">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:layout_weight="1"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:layout_weight="1"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:duplicateParentState="true"
    android:elevation="20dp"
    android:padding="10dp"
    android:orientation="horizontal">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"

        android:layout_weight="1"/>


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:duplicateParentState="true"
    android:elevation="20dp"
    android:padding="10dp"
    android:orientation="horizontal">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Button1"
        android:layout_weight="1"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Button1"
        android:layout_weight="1"/>
</LinearLayout>

</LinearLayout>