4

是否可以使用 Java 代码将按钮添加到 Activity 布局。如果这是可能的,怎么做?这是我当前的布局文件:

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

如果可能的话,我希望在 ScrollView 内的 LinearLayout 中添加 Java 按钮,但如果不可能,也可以在正常的 LinearLayout 中获得它。

我希望能够通过 Java 获取按钮的原因是我有一个包含多个对象的数组。对于每个对象,我都想有一个按钮。这个数组的大小会随着时间的推移而增加。

这是我正在使用的活动文件

package com.wuppy.minecraftmods.mods;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.wuppy.minecraftmods.R;

public class Mods extends Activity
{
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mods);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    AdView adView = (AdView) this.findViewById(R.id.ad);
    adView.loadAd(new AdRequest());
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void enterPeacefulPack(View view)
{
    Intent intent = new Intent(this, ModPeacefulpack.class);
    startActivity(intent);
}
}

所以我想通过Java添加按钮,因为我不能在xml中真正做到这一点。这可能吗?如果可以,怎么办?

4

3 回答 3

4

是的,可以在 XML 文件中为 LinearLayout 定义一个 id。让我们说:

android:id="@+id/buttonContainer"

然后在Activity java代码中设置contentView后找到这个id:

LinearLayout buttonContainer = (LinearLayout) findViewById(R.id.buttonContainer);

然后创建你的按钮:

Button button = new Button();

给定提供的方法,根据需要自定义它。

最后将其添加到您的布局中:

buttonContainer.addView(button);
于 2013-04-01T10:19:37.047 回答
1

将适当的导入添加到您的活动中:

import android.widget.Button;

然后在 onCreate 方法中创建一个新的按钮对象:

Button myButton = new Button(this);
myButton.setText("Press Me");

最后将按钮添加到布局中:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
layout.addView(myButton);
于 2013-04-01T10:24:37.763 回答
1

此链接提供了有关如何使用 Java 代码将小部件添加到您的 Android 应用程序的完整教程。因此,这是您的按钮内容的摘要:

1)您从方法中删除该setContentView(...)onCreate()

2)你定义你的按钮是这样的:Button myButton = new Button(this);

3)您定义这样的布局:RelativeLayout myLayout = new RelativeLayout(this);

4)您将按钮添加到布局中,如下所示:myLayout.addView(myButton);

5)你设置这样的布局:setContentView(myLayout);

要将按钮添加到已由 .xml 文件定义的布局中,在您的情况下,请编写以下代码而不是上述五个步骤:

LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);
Button button = new Button(this);
layout.addView(button);
于 2015-05-27T09:36:30.757 回答