简体   繁体   中英

Adding LinearLayout to a linearlayout in a FrameLayout

I'm having trouble adding a linearlayout to another linearlayout inside a framelayout. The linearlayout with the button doesnt show up at all. I understand that in a framelayout the child elements stack behind each other, but im adding it to a linearlayout so shouldnt it just appear beneath the listview?

Thanks for the help

Here is the xml of the layout.

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dragTopView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    dtlOpen="false">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:padding="10dip" />

            <ImageView
                android:id="@+id/quick_icon"
                android:layout_width="35dip"
                android:layout_height="35dip"
                android:scaleType="centerCrop" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/contacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/contactsListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:dividerHeight="1dip" >
        </ListView>
    </LinearLayout>
</FrameLayout>

This is how i add the view programatically,

LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.contacts);
LinearLayout lin = helpers.getInterfaceHelper().createDynamicButton();
layout.addView(lin);

This is the createDynamicButton function

public LinearLayout createDynamicButton() {
    final float scale = context.getResources().getDisplayMetrics().density;
    LinearLayout llayout = new LinearLayout(context);
    llayout.setId(1);
    int padding_10 = (int)(10 * scale + 0.5f);
    int padding_5 = (int)(5 * scale + 0.5f);
    llayout.setPadding(padding_10, padding_10, padding_10, 0);
    llayout.setGravity(Gravity.CENTER_HORIZONTAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    llayout.setLayoutParams(params);
    Button button = new Button(context);
    button.setId(2);
    button.setTextColor(Color.WHITE);
    button.setTypeface(null, Typeface.BOLD);
    button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
    int padding_15 = (int)(15 * scale + 0.5f);
    button.setPadding(padding_15, padding_15, padding_15, padding_15);
    button.setBackgroundResource(R.drawable.custom_btn);
    button.setGravity(Gravity.CENTER_HORIZONTAL);
    button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    llayout.addView(button);
    return llayout;
}

FrameLayout is designed to only show one parent view in it. Replace it with a LinearLayout like below and test it.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dragTopView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:padding="10dip" />

            <ImageView
                android:id="@+id/quick_icon"
                android:layout_width="35dip"
                android:layout_height="35dip"
                android:scaleType="centerCrop" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/contacts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/contactsListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:dividerHeight="1dip" >
        </ListView>
    </LinearLayout>
</LinearLayout>

If you want to stack your views up, replace your LinearLayout with RelativeLayout and edit the height and width of the views you want. RelativeLayout enables you to stack views.

does not stack it up, it just shows 1 view.. in your case it is the LinearLayout. 不会将其堆叠起来,它仅显示1个视图。在您的情况下,它是LinearLayout。

without further declarations stack it up. 会将其堆叠起来。 Just match the Relative layout's height and width.

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