在Java代码中为Android添加按钮
是否可以使用Java代码向活动布局添加按钮。如果这是可能的,如何实现? 这是我当前的布局文件:在Java代码中为Android添加按钮,java,android,button,Java,Android,Button,是否可以使用Java代码向活动布局添加按钮。如果这是可能的,如何实现? 这是我当前的布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ad_catalog_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:backgrou
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ad_catalog_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<com.google.ads.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
googleads:adSize="IAB_BANNER"
googleads:adUnitId="a14d7f7d2180609" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/menu_mods"
android:textColor="#FFFFFF"
android:textSize="25sp" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="enterPeacefulPack"
android:text="@string/peacefulpack"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
android:textColor="#FFFFFF"
android:textSize="25sp" />
</LinearLayout>
</ScrollView>
所以我想通过Java添加按钮,因为我不能在xml中真正做到这一点。这可能吗?如果可能,怎么可能 是。如果可能,请在XML文件中为线性布局定义一个id。让我们说:
android:id="@+id/buttonContainer"
然后在活动java代码中,在设置contentView后查找此id:
LinearLayout buttonContainer = (LinearLayout) findViewById(R.id.buttonContainer);
然后创建您的按钮:
Button button = new Button();
根据提供的方法,根据需要进行自定义
最后将其添加到布局中:
buttonContainer.addView(button);
将适当的导入添加到您的活动中:
import android.widget.Button;
然后在onCreate方法中创建一个新按钮对象:
Button myButton = new Button(this);
myButton.setText("Press Me");
最后,将按钮添加到布局中:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
layout.addView(myButton);
提供有关如何使用java代码将小部件添加到Android应用程序的完整教程。下面是关于按钮的总结:
1) 从onCreate()
方法中删除setContentView(…)
行
2) 您可以这样定义按钮:button myButton=new按钮(this)代码>
3) 您可以这样定义布局:RelativeLayout myLayout=newrelativelayout(this)代码>
4) 将按钮添加到布局中,如下所示:myLayout.addView(myButton)代码>
5) 您可以这样设置布局:setContentView(myLayout)代码>
要将按钮添加到已由.xml文件定义的布局(在您的示例中),请编写以下代码,而不是上述五个步骤:
LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);
Button button = new Button(this);
layout.addView(button);
请参考此url。这是完全可能的,互联网上有很多关于这个话题的帖子。你为什么不使用谷歌?不管我做什么,Button-Button=new-Button()代码>在没有输入上下文的情况下不起作用。如果您是从java.awt
导入小部件,而不是从android.widget导入小部件,那么它可以工作。但是,除非我使用(this)
作为上下文,否则new Button()在java中不起作用。你能解释一下为什么只显示button()?谢谢。这是一个错误,Android视图需要一个上下文。
LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);
Button button = new Button(this);
layout.addView(button);