扩展按钮android,xml布局
我有以下类(包含在另一个类中)
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
按钮的显示是使用以下代码进行的:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
如何将按钮布局定义到 .xml 文件中,而不是在 java 代码中执行它?
我已经尝试过:
<AudioRecordTest.test.RecordButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/record" />
但它不起作用......
非常感谢,
约阿希姆
I have the following class (included into another class)
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
The display of the button is made using the following code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
How can I define the Button layout into the .xml file instead of doing it in the java code?
I have tried that:
<AudioRecordTest.test.RecordButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/record" />
But it is not working...
Many thanks,
Joachim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我理解“(包含在另一个类中)”,因为您有一个内部类
RecordButton
。假设您的包是
AudioRecordTest.test
(这将是一个非常糟糕的名称选择)并且您的 RecordButton 类是 AudioRecord.class 的内部类,您需要使用:使用
$ 符号来分隔内部类。您需要将限定名称写在引号内。另外,请确保您将类创建为公共静态,否则它将不可见。
顺便说一句:您将其创建为内部类而不是将其分开有什么特殊原因吗?
I understand "(included into another class)" as you have an inner class
RecordButton
.Assuming your package is
AudioRecordTest.test
(which would be a very bad name choice) and your RecordButton class is an inner class of AudioRecord.class, you need to use:Use the
$
sign to separate inner classes. You need to write the qualified name inside quotes. Also, make sure you create your class public static, or it won't be visible.BTW: any particular reason you create it as an inner class instead of having it separate?