Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android通过编程将自定义按钮添加到布局中_Android_Button_Programmatically Created - Fatal编程技术网

android通过编程将自定义按钮添加到布局中

android通过编程将自定义按钮添加到布局中,android,button,programmatically-created,Android,Button,Programmatically Created,我想通过编程在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中添加按钮,按钮的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" />

最好的方法是什么? 我将只更改每个新按钮的文本。。 也许我会有另一种按钮类型,就像其他背景和文本颜色一样。

proje-->res-->values-->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"
/>

匹配父项
#000000
20便士
左边
30便士
30便士
10便士

确定做一件事。只有您想更改按钮文本。因此,对于按钮对象,以编程方式保留setText()和setBackground()…

也可以创建此按钮xml并从代码中增加布局资源: 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" />

在布局中创建按钮,然后使用
yourButton.setVisibility(View.GONE)
用于隐藏它并使用
yourButton.setVisibility(View.VISIBLE)使其可见。

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" >

@2Dee no的可能重复项与该问题完全不同。。我想添加更多次的自定义按钮。。与自己的设置基本相同,您只需要使用此选项应用自定义样式,或者通过编程方式设置样式…如何通过编程方式设置样式?我可以知道以编程方式执行所有操作的原因吗?我有一个类别列表..并且需要以编程方式将它们一个放在另一个之下..在buttonsok中。告诉我一件事,您使用xml创建了按钮因此,您想动态地更改我提到的上述解决方案的按钮文本(或者)您想动态地创建按钮,而不在xml文件中定义?我想以编程方式添加按钮,看起来像我提供的xml中的按钮这正是我想做的,谢谢。如何以编程方式设置android:layout_below=标记,以便将按钮添加为最后一个按钮?您必须创建LayoutParams并添加规则,此链接可能会帮助您:同时考虑替换为LinearLayout。
<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" >
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;
}