扩展按钮android,xml布局

发布于 2024-11-07 23:38:59 字数 1351 浏览 0 评论 0原文

我有以下类(包含在另一个类中)

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 技术交流群。

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

发布评论

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

评论(1

丑丑阿 2024-11-14 23:38:59

我理解“(包含在另一个类中)”,因为您有一个内部类 RecordButton

假设您的包是 AudioRecordTest.test (这将是一个非常糟糕的名称选择)并且您的 RecordButton 类是 AudioRecord.class 的内部类,您需要使用:

 <view class="AudioRecordTest.test.AudioRecord$RecordButton"

使用 $ 符号来分隔内部类。您需要将限定名称写在引号内。另外,请确保您将类创建为公共静态,否则它将不可见。

顺便说一句:您将其创建为内部类而不是将其分开有什么特殊原因吗?

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:

 <view class="AudioRecordTest.test.AudioRecord$RecordButton"

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文