繁体   English   中英

将LinearLayout添加到FrameLayout中的linearlayout

[英]Adding LinearLayout to a linearlayout in a FrameLayout

我在将framelinelayout内的另一个linearlayout添加到另一个linearlayout时遇到问题。 带按钮的linearlayout根本不显示。 我知道在framelayout中,子元素彼此堆叠,但是我将其添加到linearlayout中,所以它不应该只出现在listview下吗?

谢谢您的帮助

这是布局的xml。

<?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>

这就是我以编程方式添加视图的方式,

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

这是createDynamicButton函数

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旨在仅在其中显示一个父视图。 将其替换为下面的LinearLayout并进行测试。

<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>

如果要堆叠视图,请用RelativeLayout替换LinearLayout并编辑所需视图的高度和宽度。 RelativeLayout使您可以堆叠视图。

FrameLayout不会将其堆叠起来,它仅显示1个视图。在您的情况下,它是LinearLayout。

没有进一步声明的RelativeLayout会将其堆叠起来。 只需匹配相对布局的高度和宽度即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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