android - 如何将此文本添加到 Android Studio 中的 xml 文件中?

标签 android android-studio

这段代码在我的 OnCreate() 函数中。我从网上获得了这段代码,我需要在我的 xml 文件中对其进行一些更改,例如添加背景属性和其他设计元素等,请帮助我如何传输它在我的 activity_main.xml 文件中没有任何错误(这是我将在设计部分工作的地方)。 先感谢您。

    LinearLayout ll = new LinearLayout(this);
    mRecordButton = new RecordButton(this);
//RecordButton is a class I created and mRecordButton is an object which
// inherits some porperties of it.
//Same with PlayButton and mPlayButton
    ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    0));
    mPlayButton = new PlayButton(this);
    ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    0));
    setContentView(ll);
}

最佳答案

布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <com.yourcompany.package.RecordButton
         android:id="@+id/record_btn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
    <com.yourcompany.package.PlayButton
         android:id="@+id/play_btn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

<LinearLayout/>

然后在同一个 onCreate() 方法中,删除现有代码并使用 setContentView(R.layout.layout_name)

关于android - 如何将此文本添加到 Android Studio 中的 xml 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38133969/

相关文章:

android - 在 Android Studio 中显示打开选项卡的列表

android-studio - Android Studio 不提供 SQL 作为注入(inject)语言

android - 在 App Billing 服务中有时会被杀死

android - 如何在 Android 中正确加载存档网页?

java - ANDROID获取网络上的所有IP地址

android-studio - 如何选择一组特定的测试(不在同一包中)在 Android Studio 中运行?

git - git clone 后的 android studio gradle

android - 单击状态通知打开特定文件夹

android - 没有 LifecycleOwner 引用的观察室表

Android SpeechRecognizer 额外 "calling_package"缺失