【发布时间】:2017-04-12 23:36:13
【问题描述】:
在您将我标记为重复并开始投票之前,请知道我正在尽我所能在这里让它工作近 3 个小时。我尝试了四种不同的方法,我在文档和各种论坛主题中了解到。
我在独立的xml 文件中定义了Button,如下所示:
<!--button_template.xml-->
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/score_question_btn"
android:onClick="viewScore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:padding="24dp" />
我想用它来动态填充视图。
到目前为止我所尝试的:
-
context.findViewById(R.id.button_id);这不起作用,因为按钮不是当前上下文根视图的子视图,因此返回null -
LayoutInflator->inflate(R.layout.button_template.xml, rootView, false);我不能用这个,因为我需要为特定的按钮设置不同的文本和背景颜色。 - 使用
style resource的自定义,定义margins,但我找不到设置Button style的方法 -
Button button = new Button(context)简而言之,我无法让它工作。我创建了Button,我可以轻松设置文本和颜色,但是有margins的问题。
经过半小时的尝试,将 margin 放在 Button 上,我想出了这个:
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int dpMargin = 16;
float d = context.getResources().getDisplayMetrics().density;
int marginInPixels = (int) (dpMargin * d);
params.setMargins(marginInPixels, marginInPixels, marginInPixels, 0);
但我不知道它是否有效,因为这一直在使我的项目崩溃。它执行一次,然后崩溃,然后我无法启动我的项目,因为它找不到我的MainActivity class。我也花了一个小时来追踪这个。我想出的唯一解决方法是将我的 src 文件夹复制到新项目。
那么问题来了:我是否走在正确的轨道上?如果是这样,我做错了什么?如果不是 - 有经验的 android 开发人员将如何处理这个模板问题。
【问题讨论】:
-
“因为我需要为特定按钮设置不同的文本和背景颜色”——您可以在
Button实例上调用 Java 方法来设置此信息。 -
我不确定我是否理解您想要实现的目标...您说“我想使用它来动态填充视图。”。我猜您正在尝试创建一个按钮布局 xml 并在应用程序的其他部分/其他布局内部、一些线性布局等中重用该按钮?
标签: java android xml dynamic templating