如何正确地在 android 中进行模板化/xml 重用
How to do templating / xml reusing in android properly
在你将我标记为重复并开始投票之前,请知道我正在尽我所能让这个工作现在将近 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);
这不起作用,因为该按钮不是当前上下文根视图的子视图,因此 returns 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 开发人员将如何处理这个模板问题。
我会 extend
按钮 class..
这是一个例子
MyReusableButton.java
package com.example.test;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyReusableButton extends Button {
//use this constructor for button creation from java code
public MyReusableButton(Context context) {
super(context);
init();
}
//this is needed for XML inflation
public MyReusableButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
//set button style
private void init() {
setBackgroundColor(Color.RED);
setTextColor(Color.WHITE);
}
//helper to set margins
public void setMargins(int left, int top, int right, int bottom) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
this.setLayoutParams(params);
}
}
MainActivity.java
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//use this to create a button in code
MyReusableButton b = new MyReusableButton(this);
b.setText("Hello, World");
//use this to add margins to the button
b.setMargins(10, 10, 10, 10);
//add the button to the parent linear layout
((LinearLayout) findViewById(R.id.wrapper)).addView(b);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We can also define a button in XML -->
<com.example.test.MyReusableButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Test2"
/>
</LinearLayout>
此代码的结果是 2 个按钮,一个来自 XML,一个以编程方式创建。 init()
方法为我们提供了每个按钮所需的样式。
我还包含了一个辅助方法来设置边距以节省将来的代码。
在你将我标记为重复并开始投票之前,请知道我正在尽我所能让这个工作现在将近 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);
这不起作用,因为该按钮不是当前上下文根视图的子视图,因此 returnsnull
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 开发人员将如何处理这个模板问题。
我会 extend
按钮 class..
这是一个例子
MyReusableButton.java
package com.example.test;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyReusableButton extends Button {
//use this constructor for button creation from java code
public MyReusableButton(Context context) {
super(context);
init();
}
//this is needed for XML inflation
public MyReusableButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
//set button style
private void init() {
setBackgroundColor(Color.RED);
setTextColor(Color.WHITE);
}
//helper to set margins
public void setMargins(int left, int top, int right, int bottom) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
this.setLayoutParams(params);
}
}
MainActivity.java
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//use this to create a button in code
MyReusableButton b = new MyReusableButton(this);
b.setText("Hello, World");
//use this to add margins to the button
b.setMargins(10, 10, 10, 10);
//add the button to the parent linear layout
((LinearLayout) findViewById(R.id.wrapper)).addView(b);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We can also define a button in XML -->
<com.example.test.MyReusableButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Test2"
/>
</LinearLayout>
此代码的结果是 2 个按钮,一个来自 XML,一个以编程方式创建。 init()
方法为我们提供了每个按钮所需的样式。
我还包含了一个辅助方法来设置边距以节省将来的代码。