添加Android的一个按钮,在Java中code [英] Adding a button for Android in Java code
问题描述
是否有可能一个按钮添加到使用Java code的活动布局。如果这是可能的,怎么样?
这是我目前的布局文件:
Is it possible to add a button to an Activity layout with Java code. If this is possible, how? This is my current layout file:
<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>
如果有可能,我想有即滚动型的内部的LinearLayout内的附加的Java按钮,但如果这是不可能的,也将是可能的得到它在正常的LinearLayout
If it is possible I would like to have the Java-added button inside the LinearLayout that is inside of the ScrollView, but if that isn't possible it would also be possible to get it in the normal LinearLayout.
为什么我希望能够通过Java来获得按钮的原因是,我有一个包含多个对象的数组。对于每一个对象,我想有一个按钮。这个阵列会扩大规模随着时间的推移。
The reason why I want to be able to get buttons through Java is that I have an Array that contains several objects. For every object I would like to have a button. This Array will increase in size over time.
这是我使用的活动文件
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。这是可能的,如果是怎样?
So I want to add buttons through Java since I can't really do this in xml. Is that possible and if so how?
推荐答案
是的,它可能在你的XML文件中定义一个id为你的LinearLayout。让说:
Yes it possible, define an id to your LinearLayout in your XML file. lets say:
android:id="@+id/buttonContainer"
然后在活动的java code发现这个ID设置的内容查看后:
Then in the Activity java code find this id after setting the contentView:
LinearLayout buttonContainer = (LinearLayout) findViewById(R.id.buttonContainer);
然后创建你的按钮:
Then create your button:
Button button = new Button();
定制你喜欢给提供的方法。
customize it as you like, given the methods provided.
最后把它添加到您的布局:
And finally add it to your layout:
buttonContainer.addView(button);
这篇关于添加Android的一个按钮,在Java中code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!