【问题标题】:Adding a button for Android in Java code在 Java 代码中为 Android 添加一个按钮
【发布时间】:2013-04-01 10:13:57
【问题描述】:

是否可以使用 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 获取按钮的原因是我有一个包含多个对象的数组。对于每个对象,我都想有一个按钮。这个数组的大小会随着时间的推移而增加。

这是我正在使用的 Activity 文件

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 中真正做到这一点。这可能吗?如果可以,怎么办?

【问题讨论】:

标签: java android button


【解决方案1】:

是的,可以在您的 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);

【讨论】:

  • 无论我做什么,如果不输入上下文,Button button = new Button(); 将无法工作。如果您从java.awt 导入小部件而不是android.widget,它会起作用。但除非我使用 (this) 作为上下文,否则 new Button() 在 java 中不起作用。你能解释一下为什么你只显示 button(); ?谢谢。
  • @dbconfession 这是一个错误,Android 视图需要上下文。
【解决方案2】:

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

import android.widget.Button;

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

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

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

LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
layout.addView(myButton);

【讨论】:

    【解决方案3】:

    This link 提供了有关如何使用 java 代码将小部件添加到您的 Android 应用程序的完整教程。所以,这里是你的按钮的总结:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2020-11-28
      相关资源
      最近更新 更多