android - 将按钮添加到 LinearLayout

标签 android android-linearlayout

我正在尝试动态添加一个按钮到 LinearLayout 。这是我的代码:

JAVA

    LayoutInflater inflater = mMainActivity.getLayoutInflater();
    View layout = inflater.inflate(R.layout.browse_list_fragment, (ViewGroup) getView(), false);
    LinearLayout breadcrumb = (LinearLayout) layout.findViewById(R.id.browse_list_fragment_previous);

    Button button = new Button(mMainActivity);
    button.setText(name);
    button.setTextColor(mMainActivity.getResources().getColor(R.color.action_bar_breadcrumb));
    button.setTextSize(22);
    button.setTypeface(Typeface.createFromAsset(mMainActivity.getAssets(), "HelveticaNeueBold.ttf"));
    button.setTag(mHashMap);

    breadcrumb.addView(button, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));


XML

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

<LinearLayout android:id="@+id/browse_list_fragment_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/white">

    <Button android:id="@+id/browse_list_fragment_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="@string/button_menu"
        android:textColor="@color/grey"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/carrot_grey"
        android:onClick="buttonClick"/>
</LinearLayout>

<LinearLayout android:id="@+id/browse_list_fragment_previous"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical">

</LinearLayout>

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingLeft="5dp"/>
</LinearLayout>


问题
我没有抛出任何异常,当我连接调试器并单步执行代码时,我可以看到该按钮实际上已添加到布局中。我尝试过膨胀一个按钮并添加它,Java和XML代码的不同组合,删除代码行,使用RelativeLayout作为根布局,删除布局的不同部分并使用不同的宽度和高度,但我无法做到这一点按钮显示在屏幕上。有人可以看到我做错了什么或者至少指出我正确的方向吗?我将不胜感激。

最佳答案

您调用了 inflater.inflate(),并将 false 作为最后一个参数:

View layout = inflater.inflate(R.layout.browse_list_fragment,
    (ViewGroup) getView(), false);

因此,您没有将布局添加到任何 View ,因此看不到添加到此布局的按钮。尝试使用 true 调用 inflate() 或稍后将布局添加到 View 中。

关于android - 将按钮添加到 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032341/

相关文章:

android - Android 中的自定义适配器

android - 在 LinearLayout 中动态添加 View 水平和垂直

android - 导致 Android 应用程序出现问题的 SSL 证书

android - Android 上是否有 "add to homescreen"webapp(就像在 iOS 中一样)

android - 按钮布局成一圈?

android - 如何在 `PhotoView` 上显示文字?

RTL 中的 android:gravity ="start"将文本左对齐而不是右对齐

android - 在Android中更改LinearLayout的背景

java - Android 从两个 Activity 传递哈希表

android - 无法在 Android 2.3.3 中获取电话号码