简体   繁体   中英

Incorrectly displayed view created programmatically (android)

I create some views programmatically, but on some devices they are displayed wrong. I have Layout in which i programmatically add spinner. This is my xml:

container - it's layout where i create spinner (programmatically)

spinnerL - here i add spinner

<LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="#26ffffff"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/spinnerL"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_weight="0.2"
            android:layout_centerHorizontal="true" />

        <RelativeLayout
            android:id="@+id/spinnerOpen"
            android:layout_width="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="0.8"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/spinnerImage"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />



        </RelativeLayout>

    </LinearLayout>

Here i create and add the spinner:

//here i set margins
View linearLayoutG = convertView.findViewById(R.id.container);

linearLayoutG.setBackgroundColor(Color.parseColor("#26ffffff"));

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(10, 0, 10, 30);
linearLayoutG.setLayoutParams(layoutParams);

//here i create spinner
View linearLayout = convertView.findViewById(R.id.spinnerL);
final Spinner spinner = new Spinner(context);

spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

//code

((LinearLayout) linearLayout).addView(spinner);

(sorry about links, i don't have 15 reputation)

Result on android 4.0.1

Result on android 5+

If i use static size, like this:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, 120);

Here's what happens

Result on android 4.0.1

Result on android 5+

Well as i can see in your code :

spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

you are setting the height to MATCH_PARENT . I have no idea why it is working on 4.0.1 but change your code to this:

spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM