WRAP_CONTENT not working after dynamically adding views

后端 未结 8 884
借酒劲吻你
借酒劲吻你 2020-12-05 02:28

I\'m trying to create a fragment that lays out a series of custom views dynamically. The main content for this layout is a RelativeLayout nested in a LinearLayout (to center

相关标签:
8条回答
  • 2020-12-05 02:32

    A simple way to update the size of a View with WRAP_CONTENT is change the visibility to GONE and back to the old visibility.

    int visibility = view.getVisibility();
    view.setVisibility(View.GONE);
    view.setVisibility(visibility);
    

    TESTED ON ANDROID JELLY BEAN IN 2014

    MAY NOT WORK ON NEWER ANDROID VERSIONS

    0 讨论(0)
  • 2020-12-05 02:35

    My problem was fixed by seting layout_width to some specific dp.

    So changing from "wrap content" or "match parent" to

    android:layout_width="300dp"
    

    will fix it, but i know it's not solution for all cases. But maybe you have some parent width, so you can apply the width to the textview. height leave with wrap_content, and it will work.

    0 讨论(0)
  • 2020-12-05 02:36

    Ok, I solved this by manually measuring the RelativeLayout immediately after adding all the views and setting the mainLayoutParams height explicitly. I wish I was smarter and knew why it wasn't automatically doing this correctly in the first place, but oh well.

        ...
        mainLayout.measure(0, 0);
    
        ViewGroup.LayoutParams mainLayoutParams = mainLayout.getLayoutParams();
        mainLayoutParams.height = mainLayout.getMeasuredHeight() + 10;
        ...
    
    0 讨论(0)
  • 2020-12-05 02:39

    Set the layout parameters again(width and height), right after adding the view. This worked for me.

    if the parent View is a FrameLayout, then do something like this:

        ImageView view = (ImageView) LayoutInflater.from(activity).inflate(R.layout.image_object_view, null);
    
        imageObjectsHolder.addView(view);
        FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(param);
    
    0 讨论(0)
  • 2020-12-05 02:46

    You should use NestedScrollView Instead of simple scrollview. here is my sample Activity Layout code

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F0ECE6"
        >
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="fixed"
                app:tabIndicatorHeight="6dp"
                android:layout_marginTop="-10dp"
                app:tabGravity="fill"/>
        </android.support.design.widget.AppBarLayout>
    
        <wsit.rentguru.utility.CustomViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
    </android.support.design.widget.CoordinatorLayout>
    

    here is code for custom viewpager

    public class CustomViewPager extends ViewPager {
    
            private boolean enabled;
    
            public CustomViewPager(Context context, AttributeSet attrs) {
                super(context, attrs);
                this.enabled = false;
            }
    
            @Override
            public boolean onTouchEvent(MotionEvent event) {
                if (this.enabled) {
                    return super.onTouchEvent(event);
                }
    
                return false;
            }
    
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                if (this.enabled) {
                    return super.onInterceptTouchEvent(event);
                }
    
                return false;
            }
    
            public void setPagingEnabled(boolean enabled) {
                this.enabled = enabled;
            }
        }
    

    setup function for viewpager in the activity

    private void setupViewPager(ViewPager viewPager) {
            ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
            adapter.addFragment(new SampleFragment(), " ");
    
            viewPager.setAdapter(adapter);
        }
    
    class ViewPagerAdapter extends FragmentPagerAdapter {
            private final List<Fragment> mFragmentList = new ArrayList<>();
            private final List<String> mFragmentTitleList = new ArrayList<>();
    
            public ViewPagerAdapter(FragmentManager manager) {
                super(manager);
            }
    
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }
    
            @Override
            public int getCount() {
                return mFragmentList.size();
            }
    
            public void addFragment(Fragment fragment, String title) {
                mFragmentList.add(fragment);
                mFragmentTitleList.add(title);
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                return mFragmentTitleList.get(position);
            }
        }
    

    here is the sample SampleFragment layout code

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F0ECE6"
        android:fillViewport="true"
        android:scrollbars="vertical"
        android:animateLayoutChanges="true"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F0ECE6"
        android:focusableInTouchMode="true"
       >
    
        <Spinner
            android:id="@+id/product_category"
            android:layout_margin="20dp"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext_rectangle_box"
            android:gravity="center|left"
            android:textSize="14sp"
            android:paddingLeft="10dp"
            android:drawableRight="@drawable/ic_down_arrow"
            />
    
        <Spinner
            android:id="@+id/product_sub_category"
            android:layout_below="@+id/product_category"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center|left"
            android:visibility="gone"
            android:paddingLeft="10dp"
            android:background="@android:color/white"
            android:drawableRight="@drawable/ic_down_arrow"
            />
        <EditText
            android:id="@+id/product_title"
            android:layout_below="@+id/product_sub_category"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="15dp"
            android:background="@android:color/white"
            android:hint="PRODUCT TITLE"
            android:singleLine="true"
            android:imeOptions="actionDone"
            android:layout_width="match_parent"
            android:gravity="center|left"
            android:padding="10dp"
            android:textSize="14sp"
            android:textColorHint="#000000"
            android:layout_height="40dp" />
    
        <LinearLayout
            android:id="@+id/availability_layout"
            android:layout_below="@+id/product_title"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/availability"
                android:textStyle="bold"
                android:paddingBottom="10dp"
                android:textSize="14sp"
                />
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="2">
    
                <Button
                    android:id="@+id/from"
                    android:background="@android:color/white"
                    android:hint="FROM"
                    android:layout_weight="1"
                    android:gravity="center|left"
                    android:padding="10dp"
                    android:textSize="14sp"
                    android:textStyle="normal"
                    android:textColorHint="#000000"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginRight="10dp"/>
    
                <Button
                    android:id="@+id/to"
                    android:background="@android:color/white"
                    android:hint="TO"
                    android:layout_weight="1"
                    android:gravity="center|left"
                    android:padding="10dp"
                    android:textSize="14sp"
                    android:textColorHint="#000000"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"/>
    
    
    
            </LinearLayout>
    
    
        </LinearLayout>
    
    
        <LinearLayout
            android:id="@+id/product_location_layout"
            android:layout_below="@+id/availability_layout"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/product_location"
                android:textStyle="bold"
                android:paddingBottom="10dp"
                android:textSize="14sp"
                />
    
    
            <Spinner
                android:id="@+id/state_spinner"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@drawable/edittext_rectangle_box"
                android:gravity="center|left"
                android:textSize="14sp"
                android:layout_marginBottom="10dp"
                android:drawableRight="@drawable/ic_down_arrow"
                android:paddingLeft="10dp"
                />
    
            <EditText
                android:id="@+id/area"
                android:background="@android:color/white"
                android:hint="Area"
                android:gravity="center|left"
                android:padding="10dp"
                android:textSize="14sp"
                android:textColorHint="#000000"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:singleLine="true"
                android:imeOptions="actionNext"
                android:layout_marginBottom="10dp"
                />
    
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="2">
    
                <EditText
                    android:id="@+id/zipCode"
                    android:background="@android:color/white"
                    android:hint="Zip Code"
                    android:layout_weight="1"
                    android:gravity="center|left"
                    android:padding="10dp"
                    android:textSize="14sp"
                    android:textColorHint="#000000"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:singleLine="true"
                    android:imeOptions="actionNext"
                    android:layout_marginRight="10dp"/>
    
                <EditText
                    android:id="@+id/city"
                    android:background="@android:color/white"
                    android:hint="City"
                    android:layout_weight="1"
                    android:gravity="center|left"
                    android:padding="10dp"
                    android:textSize="14sp"
                    android:singleLine="true"
                    android:imeOptions="actionDone"
                    android:textColorHint="#000000"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"/>
    
    
    
            </LinearLayout>
    
    
        </LinearLayout>
    
        <Button
            android:id="@+id/tab1_next"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:text="NEXT"
            android:layout_below="@+id/product_location_layout"
            android:layout_margin="20dp"
            android:layout_alignParentRight="true"
            android:background="@color/next_button"
            android:textColor="@android:color/white"
            android:layout_marginBottom="20dp"
            />
    
    </RelativeLayout>
    
    </android.support.v4.widget.NestedScrollView>
    
    0 讨论(0)
  • 2020-12-05 02:48

    Android does NOT refresh layout of views with "wrap_content" once it has been displayed.

    So if you add a child view, or modify the content dynamically, you're screwed. I do agree that this is a nightmare and a real flaw in Android UI!

    To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with "wrap_content"

    The code and instructions to use are available here:

    https://github.com/ea167/android-layout-wrap-content-updater

    Enjoy!

    0 讨论(0)
提交回复
热议问题