Android 将按钮添加到LinearLayout
我正在尝试向LinearLayout动态添加一个按钮。这是我的密码: JAVAAndroid 将按钮添加到LinearLayout,android,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) lay
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);
因此,您没有将布局添加到任何视图,因此无法看到添加到此布局的按钮。尝试使用
true
调用inflate()
,或稍后将布局添加到视图中。您是否尝试过:breadcrumb.addView(按钮)代码>就这样?另外,您是否尝试将该按钮放入XML文件中,并检查它是否实际显示且未被其他元素阻止?您显示的XML与您放入setCOntentView中的XML相同?非常感谢!那么,为什么当我在onCreateView()中时,我可以调用充气(layout.id、container、false)并在那里工作呢?我不知道,它不应该工作。。。可能您错过了setContentView(R.layout.browse\u list\u fragment)代码>在那里?我在一个片段中,所以没有setContentView
。。。我在onCreateView
中放大了布局,但我传入了false
,令人惊讶的是它一直在工作。哦,好吧。。。我从不处理碎片。在onCreateView()
中返回视图时,始终显示该视图。但是,如果以后要更改GUI,则必须将创建的视图附加到另一个视图,方法是使用true调用inflate
,或者稍后手动添加它。