网格布局不显示项目

时间:2014-02-05 05:40:05

标签: android android-gridlayout

XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".WRActivity" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:text="Mahinda Electricals"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="25px" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="30px" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="31dp"
            android:layout_toLeftOf="@+id/textView1"
            android:text="Number Of Fields"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="20px" />

        <EditText
            android:id="@+id/add_no_of_returns"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView3"
            android:layout_alignLeft="@+id/textView1"
            android:layout_marginLeft="22dp"
            android:ems="3"
            android:inputType="number" />

        <Button
            android:id="@+id/add_returns"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignBaseline="@+id/textView3"
            android:layout_alignBottom="@+id/textView3"
            android:layout_toRightOf="@+id/textView2"
            android:text="Add" />

        <GridLayout
            android:id="@+id/returnshow"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_below="@+id/add_returns"
            android:layout_marginTop="177dp"
            android:layout_toRightOf="@+id/button1"
            android:orientation="vertical" />

    </RelativeLayout>

Java代码:

 public class WRActivity extends Activity {
        Button add_returns;
        GridLayout show_warennt_returns;
        EditText serial_no;
        EditText reason;
        EditText no_of_returns;
        Button next;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_wr);
            add_returns = (Button) findViewById(R.id.add_returns);
            add_returns.setOnClickListener(oc);
            no_of_returns = (EditText) findViewById(R.id.add_no_of_returns);
            show_warennt_returns = (GridLayout) findViewById(R.id.returnshow);

        }



        OnClickListener oc = new OnClickListener() {

            @Override
            public void onClick(View v) {

                Add_text();
            }
        };

        public void Add_text() {
            for (int i = 0; i < Integer
                    .parseInt(no_of_returns.getText().toString()); i++) {
                GridLayout ll = new GridLayout(WRActivity.this);
                ll.setOrientation(GridLayout.HORIZONTAL);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                ll.setColumnCount(2);
                ll.setRowCount(2);

                TextView serial = new TextView(WRActivity.this);
                serial.setText("Serial No");
                serial.setPadding(10, 10, 10, 10);
                serial.setTag("weight");

                ll.addView(serial);

                serial_no = new EditText(WRActivity.this);
                serial_no = new EditText(WRActivity.this);
                serial_no.setPadding(10, 10, 10, 10);
                serial_no.setWidth(300);
                serial_no.setTag("Product_ID");
                ll.addView(serial_no);

                TextView serial1 = new TextView(WRActivity.this);
                serial1.setText("Reason");
                serial1.setPadding(10, 10, 10, 10);
                serial1.setTag("weight");
                ll.addView(serial1);

                reason = new EditText(WRActivity.this);
                reason.setPadding(10, 10, 10, 10);
                reason.setTag("Product_ID");
                reason.setWidth(300);
                ll.addView(reason);

                show_warennt_returns.addView(ll);
            }

        }


    }

我已经尝试过上面的代码,当在EditText中键入no元素时动态显示两个文本字段。

它很好但是如果我输入的数量超过10只它只显示前10个,只有其他的不显示在网格布局上。

3 个答案:

答案 0 :(得分:2)

Grid View中,您将根据屏幕类型或网格视图的长度和广度获取..

如果你想要超过10个是动态的...... 您需要使用List View来帮助您显示异步或动态数据..

如果您想在Grid中显示它们,那么您可以尝试使用Grid Lists

这适用于任意数量的动态数据。

根据您的要求,您需要将它们放在RelativeLayoutLinearLayout .. ...

如果超过10且未显示,则需要保留Scroll View

例如,您可以在此处修改代码通过放置Scroll view,.. 像... enter image description here

其中可能包含Lineary布局..如果这不适用于您的Gridlayout您可以使用前面提到的ListviewGridList

如果你只想要网格,你可以使用网格列表..它可以工作..

答案 1 :(得分:1)

更改GridLayout,如:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add_returns"
    android:layout_toRightOf="@+id/button1" >

    <GridLayout
        android:id="@+id/returnshow"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginTop="177dp"
        android:orientation="vertical" />
</ScrollView>

您的整个XML就像:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".WRActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="Mahinda Electricals"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="25px" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/title"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30px" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="31dp"
        android:layout_toLeftOf="@+id/textView1"
        android:text="Number Of Fields"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20px" />

    <EditText
        android:id="@+id/add_no_of_returns"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginLeft="22dp"
        android:ems="3"
        android:inputType="number" />

    <Button
        android:id="@+id/add_returns"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toRightOf="@+id/textView2"
        android:text="Add" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/add_returns"
        android:layout_toRightOf="@+id/button1" >

        <GridLayout
            android:id="@+id/returnshow"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="177dp"
            android:orientation="vertical" />
    </ScrollView>

</RelativeLayout>

答案 2 :(得分:1)

使用以下代码进行布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".WRActivity" >
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:text="Mahinda Electricals"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="25px" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Title"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30px" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="31dp"
    android:layout_toLeftOf="@+id/textView1"
    android:text="Number Of Fields"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="20px" />

<EditText
    android:id="@+id/add_no_of_returns"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/textView1"
    android:layout_marginLeft="22dp"
    android:ems="3"
    android:inputType="number" />

<Button
    android:id="@+id/add_returns"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_toRightOf="@+id/textView2"
    android:text="Add" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add_returns"
    android:layout_marginTop="10dp" >

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

        <GridLayout
            android:id="@+id/returnshow"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="20dp"
            android:columnCount="1"
            android:orientation="vertical" >
        </GridLayout>
    </LinearLayout>
</ScrollView>