How to use addView to add view to layout?

后端 未结 4 2189
南笙
南笙 2021-02-18 16:36

I have read probably all posts and documentation but I still can\'t solve this issue.

I want to use addView() method to add view to the existing (running) l

相关标签:
4条回答
  • 2021-02-18 16:49

    I can't remember, but maybe there is any "refresh" method to load again the layout.

    Try layout.invalidate();

    0 讨论(0)
  • 2021-02-18 17:02

    I had the same problem and wasted several time finding the reason.

    My fault was that I was adding a CustomView that had match_parent set as height.

    I hope to save some precious time to anybody who did this silly annoying mistake :)

    TextView tv = new TextView(this);
    lytMarks.addView(tv);
    
    
    CustomView cv = new CustomView(this, someObject);
    lytMarks.addView(cv); // <-- This one had height = match_parent!!
    
    
    EditText et = new EditText(this);
    lytMarks.addView(et);
    
    0 讨论(0)
  • 2021-02-18 17:09

    You forgot to specify the LayoutParameters for the newly added view.

    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
        TextView text=new TextView(this);
        text.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        text.setText("test");
        layout.addView(text);
    

    EDIT

    The GridView with an id of @+id/gridview is defined with a layout height of fill_parent, leaving you with no space to add a new view. Changing its height to wrap_content may solve your problem.

    Adding my comment to this post to help others easily verify the solution.

    0 讨论(0)
  • 2021-02-18 17:15

    Your linearLayout content is over the parent's View. so you need to use ScrollView. like this:

    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <LinearLayout
        android:id="@+id/mainLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/main1"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/app_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/app_title"
            android:textColor="#FFF"
            android:textSize="25dp" />
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"
            android:text="@string/main_screen_counter_title"
            android:textColor="#FFF"
            android:textSize="15dp" />
    
        <TextView
            android:id="@+id/frontScreenCounter"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/reading"
            android:textColor="#FFF"
            android:textSize="33dp" />
    
        <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:columnWidth="90dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:textColor="#888"
            android:verticalSpacing="10dp" />
    </LinearLayout>
    </ScrollView>
    
    0 讨论(0)
提交回复
热议问题