动态为 ImageButton 添加样式

发布于 2024-11-30 06:32:17 字数 97 浏览 7 评论 0原文

我已动态地将图像按钮添加到我的 Tablerow 中,现在我想将图像按钮的样式定义为我之前定义的样式。我尝试过 setTheme 方法,但似乎不起作用

感谢您的帮助

I have added a imagebutton dynamically to my Tablerow, now i want to define the style for the image button to a style that i have previously defined. I have tried the setTheme method that doesnot seem to work

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-12-07 06:32:17

经过大量搜索后,这就是我发现的并且效果完美。

1) 如果您想在运行时创建具有预定义样式的按钮/imagebutton/anyView,则只需按照以下步骤操作即可

2a) 在 res/layout 中创建一个新的 xml 文件,然后放入您要为其创建的一个元素例如我想创建一个减号按钮。这是 res/layout 中的 minusb.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            style="@style/MinusButton"/>

2b) 然后将以下行添加到您要创建它的 java 活动代码中

ImageButton bt = (ImageButton) getLayoutInflater().inflate(R.layout.minusb, null);
bt.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0f));
    tr.addView(bt);

k after a lot of searching this is what i found and it works perfectly.

1) if you want to create a button/imagebutton/anyView at runtime with a predefined style the you can do so just follow the steps below

2a) Create a new xml file in res/layout and put that one element you want to create for example I wanted to create a minusButton. This is minusb.xml in res/layout

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            style="@style/MinusButton"/>

2b) Then add the following lines to your java activity code where you would like to create it

ImageButton bt = (ImageButton) getLayoutInflater().inflate(R.layout.minusb, null);
bt.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0f));
    tr.addView(bt);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文