2013-04-01 29 views
2

是否有可能一個按鈕添加到使用Java代碼的活動佈局。如果這是可能的,怎麼樣? 這是我目前的佈局文件:在Java代碼中添加針對Android按鈕

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

我希望能夠通過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可以。這是可能的,如果是這樣的話?

+0

請參閱此網址。 http://stackoverflow.com/questions/4907609/add-button-to-a-layout-programmatically – jigspatel

+1

這是完全有可能的,並且在這個主題上有很多帖子在互聯網上。你爲什麼不使用Google? – Egor

回答

2

是的,有可能,在您的XML文件中爲您的LinearLayout定義一個id。讓說:

android:id="@+id/buttonContainer" 

然後在活動的Java代碼中找到這個ID設置內容查看後:

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

然後創建您的按鈕:

Button button = new Button(); 

定製它,只要你喜歡,給提供的方法。

最後將它添加到您的佈局:

buttonContainer.addView(button); 
+0

無論我做什麼,'按鈕按鈕=新的按鈕();'沒有輸入上下文不起作用。它適用於從java.awt中導入widget而不是android.widget。但除非我使用'(this)'作爲上下文,否則新的Button()在java中不起作用。你能解釋爲什麼你只顯示按鈕(); ?謝謝。 – dbconfession

+0

@dbconfession這是一個錯誤,Android視圖需要一個上下文。 – Lesleh

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

This link給出了關於如何使用Java代碼將小部件添加到Android應用程序的完整教程。所以,這裏是爲您的按鈕事情摘要:Button myButton = new Button(this);

3)你定義諸如佈局:

1)你從onCreate()方法

2)你這樣定義你的按鈕刪除setContentView(...)線這樣的: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);