我正在構建一個soundboard應用程序,其中我在tablelayout中嵌入了一些預定義按鈕,它嵌套在相對佈局中。佈局是一個自己的片段(我使用多個選項卡爲不同的類別)。Android - 以編程方式將按鈕添加到現有佈局中
聲音和標籤工作正常,現在我要實現的功能,您生成一個新的按鈕,一旦你點擊「+」按鈕,將「+」按鈕下方移動一行新的按鈕。
有沒有辦法生成一個新的tablelayout,與已有的相同的屬性,而不必在xml文件中使用空白?
我正在構建一個soundboard應用程序,其中我在tablelayout中嵌入了一些預定義按鈕,它嵌套在相對佈局中。佈局是一個自己的片段(我使用多個選項卡爲不同的類別)。Android - 以編程方式將按鈕添加到現有佈局中
聲音和標籤工作正常,現在我要實現的功能,您生成一個新的按鈕,一旦你點擊「+」按鈕,將「+」按鈕下方移動一行新的按鈕。
有沒有辦法生成一個新的tablelayout,與已有的相同的屬性,而不必在xml文件中使用空白?
試試這個簡單的例子,修改爲按您的要求
public class MainActivity extends Activity {
Context context;
ScrollView scrollView;
LinearLayout container;
LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
layout = (LinearLayout) findViewById(R.id.LinearAdd);
}
public void addButton(View view) {
Button button = new Button(MainActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
button.setLayoutParams(lp);
button.setText("ADDED");
layout.addView(button);
}
}
And this your xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/LinearAdd"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="70"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addButton"
android:text="click to add" />
</LinearLayout>
</LinearLayout>
它工作的傢伙。 – 2014-02-27 12:02:41
如果我想將這段代碼放在MainActivity之外的單獨的Activity中,僅用於生成按鈕,我該如何從片段中調用該函數? – Stef
使用此xml爲您的片段類設置視圖,並將點擊方法放入您的片段類中。那可行 – 2014-02-27 13:05:19
這裏是我的佈局片段代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/tableLayout">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="1">
<Button
android:background="@drawable/button"
style="@style/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab01sound01"
android:layout_column="0"
android:text="@string/buttonAlan" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_below="@+id/tableLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/thirdTable">
</TableLayout>
</RelativeLayout>
這是我的第一個片段標籤的代碼
public class Tab1 extends Fragment {
private int buttonAmount = MainActivity.getButtonAmountTab1();
private Button[] button = new Button[buttonAmount + 1];
private Sound sound;
private String packageName = MainActivity.getStringPackageName();
private Button addButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sound = MainActivity.getSound();
View rootView = inflater.inflate(R.layout.fragment_fun, container, false);
//This generates the Audio functionality for each button
for (int i = 1; i < buttonAmount + 1; i++) {
String buttonID = "tab01sound0" + i;
int resID = getResources().getIdentifier(buttonID, "id", packageName);
final int audioID = getResources().getIdentifier(buttonID, "raw", packageName);
button[i] = (Button) rootView.findViewById(resID);
registerForContextMenu(button[i]);
button[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound.playSound(audioID);
}
});
}
//functionality for the "+" button
addButton = (Button) rootView.findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CreateDialog newDialog = new CreateDialog();
newDialog.AlertBox(getActivity());
}
});
return rootView;
}
}
你可以發佈這個UI的佈局 –
圖片很好,但實際上它會更好,如果你發佈了一些代碼 – Blackbelt
使用兩個rel佈局一個用於btns,另一個用於+ btn(帶有屬性波紋管),並且在第一個rel佈局中添加按鈕btns id ... – Pramod