2014-09-19 117 views
-2

我想在android系統編程添加按鈕,按鈕的XML文件將是機器人編程添加自定義按鈕佈局

<Button 
android:textStyle="bold" 
android:background="@drawable/blue" 
android:textColor="@drawable/blue_text" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/funny_excuses" 
android:id="@+id/funny" 
android:paddingBottom="10dp" 
android:paddingTop="10dp" 
android:paddingLeft="6dp" 
android:paddingRight="6dp" 
android:textSize="25sp" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" /> 

什麼是做到這一點的最好方法是什麼? 我只會改變文本爲每一個新按鈕.. 也許我會有另一個按鈕類型,像其他的背景和文本顏色..

+0

的可能重複的[機器人:編程添加按鈕的佈局(http://stackoverflow.com/questions/11710200/android-編程方式添加-按鈕到一個佈局) – 2Dee 2014-09-19 12:35:55

+0

@ 2Dee沒有,一點都不像這個問題..我想添加自定義按鍵多次。與自己的設置 – 2014-09-19 12:37:43

+0

基本上它是* *同樣的事情,你只需要應用這個http://stackoverflow.com/questions自定義樣式/ 2016249 /如何以編程方式設置樣式屬性在視圖中或以編程方式設置樣式... – 2Dee 2014-09-19 12:40:47

回答

1

而且它可以創建這個按鈕的XML並從代碼充氣佈局recource: button.xml:

<Button 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:textStyle="bold" 
android:background="@drawable/blue" 
android:textColor="@drawable/blue_text" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/funny_excuses" 
android:id="@+id/funny" 
android:paddingBottom="10dp" 
android:paddingTop="10dp" 
android:paddingLeft="6dp" 
android:paddingRight="6dp" 
android:textSize="25sp" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" /> 

代碼:

Button button = (Button) getLayoutInflater().inflate(R.layout.button, null); 
    button.setText("Hello world"); 
    RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll); //layout to add 
    ll.addView(button); 
+0

這正是我想要做的,謝謝。如何以編程方式設置android:layout_below =標記,以便按鈕添加爲最後一個? – 2014-09-19 12:53:24

+0

你必須創建的LayoutParams並添加規則,這個鏈接可以幫助你:[鏈接](http://stackoverflow.com/questions/10700273/relativelayout-add-rule-relativelayout-left-of-not-working)也認爲關於替換爲LinearLayout。 – 2014-09-19 13:04:39

1

幻燈 - >水庫 - >數值 - > style.xml

<style name="othername" > 
     <item name="android:layout_width">match_parent</item> 
     <item name="android:textColor">#000000</item> 
     <item name="android:textSize">20sp</item> 
     <item name="android:gravity">left</item> 
     <item name="android:layout_marginLeft">30sp</item> 
     <item name="android:layout_marginRight">30sp</item> 
     <item name="android:layout_marginTop">10sp</item> 
    </style> 

<Button 
style="@style/othername" 
/> 
+0

如何以編程方式設置樣式? – 2014-09-19 12:36:24

+0

我想知道做的一切程序 – Confuse 2014-09-19 12:44:45

+0

我有categories..and名單的原因,需要把它們放在一個編程下面other..in按鈕 – 2014-09-19 13:11:00

1

確定做一個thing.only要更改按鈕text.so,對於編程按鈕對象保持的setText()和的setBackground()...

+0

但我想添加超過1 .. – 2014-09-19 12:29:46

+0

ok.t​​ell我一件事你已經創建了按鈕使用XML文件。動態你想改變我提到上述解決方案的按鈕文本(或)你想動態地創建按鈕,而無需在XML文件中定義? – 2014-09-19 12:37:06

+0

我想以編程方式添加按鈕,看起來像我提供的xml中的那個 – 2014-09-19 12:38:13

1

在佈局中創建按鈕,然後使用爲隱藏,並使用yourButton.setVisibility(View.VISIBLE);爲使其可見。

1

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@id/llContainer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

.javafile

public void onClick(View v) { 
switch (v.getId()) { 

case R.id.btnAddARoom: 
    //add a room 
    //Find you parent layout which we'll be adding your button to: 
    LinearLayout layout = (LinearLayout) findViewById(R.id.llContainer); 

    roomName = etAddARoom.getText().toString(); 
    Button createdButton = new Button(this); 
    createdButton.setText(roomName); 
    createdButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,   LayoutParams.WRAP_CONTENT)); 
    layout.addView(createdButton); 

    //if no rooms make tvnorooms disappear 

    break; 
} 
相關問題