android以编程方式将自定义按钮添加到布局
Posted
技术标签:
【中文标题】android以编程方式将自定义按钮添加到布局【英文标题】:android programatically add customized button to a layout 【发布时间】:2014-09-19 12:18:38 【问题描述】:我想以编程方式在 android 中添加按钮,按钮的 xml 文件将是
<Button
android:textStyle="bold"
android:background="@drawable/blue"
android:textColor="@drawable/blue_text"
android:layout_
android:layout_
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" />
最好的方法是什么? 我只会更改每个新按钮的文本.. 也许我会有另一种按钮类型,比如其他背景和文本颜色..
【问题讨论】:
Android: programmatically adding buttons to a layout的可能重复 @2Dee 不,与那个问题完全不同.. 我想多次添加自定义按钮.. 使用自己的设置 基本上它是是一样的,你只需要使用这个***.com/questions/2016249/…来应用自定义样式或者以编程方式设置样式...... 【参考方案1】:还可以从代码中创建此 Button xml 和扩展布局资源: 按钮.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_
android:layout_
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);
【讨论】:
这正是我想做的,谢谢。如何以编程方式设置 android:layout_below= 标记,以便将按钮添加为最后一个? 您必须创建 LayoutParams 并添加规则,此链接可能会对您有所帮助:link 也考虑替换为 LinearLayout。【参考方案2】: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"
/>
【讨论】:
如何以编程方式设置样式? 我可以知道以编程方式做所有事情的原因 我有一个类别列表......并且需要以编程方式将它们放在其他......在按钮中【参考方案3】:ok 做一件事。只有你想改变按钮文本。所以,以编程方式为按钮对象保持 setText() 和 setBackground()...
【讨论】:
ok.告诉我一件事您已经使用 xml 文件创建了按钮。因此您想要动态更改按钮文本,我提到了上述解决方案(或)您想要动态创建按钮而不在xml文件中定义? 我想以编程方式添加看起来像我提供的 xml 中的按钮【参考方案4】:在布局中创建按钮,然后使用 yourButton.setVisibility(View.GONE);
隐藏它并使用 yourButton.setVisibility(View.VISIBLE);
使其可见。
【讨论】:
【参考方案5】:layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/llContainer"
android:layout_
android:layout_
android:orientation="vertical" >
.java文件
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;
【讨论】:
以上是关于android以编程方式将自定义按钮添加到布局的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式将自定义 uiview 添加到视图控制器 SWIFT