新手尝试使用 Android Studio 创建应用程序

问题描述 投票:0回答:1

好吧,就像标题一样,在 .apk 开发方面,我是一个完全的新手。我想为我们的 DnD 会话创建一个私人应用程序,专门用于拼写列表。我希望主菜单显示各个类别的按钮(战士、吟游诗人等)。这些按钮中的每一个都应该显示另一个页面,其中有与所选类别相关的法术级别的按钮(级别 0、级别 1 等),然后这些按钮应该显示与这些特定级别相关的法术列表。每个咒语都应列出一个简单的描述,但每个咒语都应该可单击以显示该咒语的专用页面,以获取有关特定咒语的更多信息。

现在你知道我想去哪里了,但我有一个问题。我使用 Activity_main.xml (在布局文件夹中)为 mainactivity.kt 制作了按钮。然后我尝试实现片段。我有一个名为 BardFragment.kt 的测试,其布局名为fragment_bard.xml。但是当我尝试该应用程序时,如果我单击 Bard Spell 按钮,该片段将显示在列出的按钮下,而不是出现在另一个页面或实例上。

这是我拥有的文件(是的,它是法语,我来自魁北克)

MainActivity.kt

package com.example.dndspellbooklist

import android.os.Bundle
import android.widget.Button
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

class MainActivity : AppCompatActivity() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            setContentView(R.layout.activity_main)

            val buttonbarde = findViewById<Button>(R.id.button_barde)

            buttonbarde.setOnClickListener{
                showFragment(BardeFragment())}

        }
    }

    private fun showFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .addToBackStack(null)
            .commit()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_barde"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="@string/sorts_de_barde" />

    <Button
        android:id="@+id/button_druide"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="@string/sorts_de_druide" />

    <Button
        android:id="@+id/button_ensorceleur"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="@string/sorts_d_ensorceleur" />

    <Button
        android:id="@+id/button_magicien"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="@string/sorts_magicien" />

    <Button
        android:id="@+id/button_paladin"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="@string/sorts_de_paladin" />

    <Button
        android:id="@+id/button_pretre"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="@string/sorts_de_pr_tre" />

    <Button
        android:id="@+id/button_rodeur"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="@string/sorts_de_r_deur" />

    <!-- Fragment container to display the selected fragment -->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

BardeFragment.kt

package com.example.dndspellbooklist

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.dndspellbooklist.R

class BardeFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_barde, container, false)


    }
}


和fragment_barde.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="300dp"
    android:layout_gravity="center"
    android:layout_marginTop="50dp"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_b0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_0" />

    <Button
        android:id="@+id/button_b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_1" />

    <Button
        android:id="@+id/button_b2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_2" />

    <Button
        android:id="@+id/button_b3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_3" />

    <Button
        android:id="@+id/button_b4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_4" />

    <Button
        android:id="@+id/button_b5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_5" />

    <Button
        android:id="@+id/button_b6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_6" />

    <Button
        android:id="@+id/button_b7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_7" />

    <Button
        android:id="@+id/button_b8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_8" />

    <Button
        android:id="@+id/button_b9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/level_9" />
</LinearLayout>

您对可能出现的问题有什么见解吗?

附注我尝试过 ChatGPT 和 youtube 教程,还有一些帖子......但我对 Kotlin 和 Java 完全是新手。我唯一的经验是 HTML 和 VB

android kotlin android-fragments
1个回答
0
投票

在我看来,在activity_main.xml上,你应该只保留framelayout,将其余部分放在另一个xml上,调用它例如first_fragment.xml并编写类似的代码

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    val frag = Fragment(R.layout.first_fragment)
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, frag)
            .addToBackStack(null)
            .commit()
    }

    fun showFragment(view: View?) {
    var frag: Fragment
        when (view!!.id) {
            R.id.button_barde -> {
                frag = Fragment(R.layout.fragment_barde)
            }
            R.id.button_druide -> {
                frag = Fragment(R.layout.button_druide)
            }
    // other buttons
        }
        val ft = supportFragmentManager.beginTransaction()
        ft.replace(R.id.settings_container, frag)
        ft.addToBackStack(null)
        ft.commit()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Fragment container to display the selected fragment -->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

first_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_barde"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
    android:onClick="showFragment"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="@string/sorts_de_barde" />

    <Button
        android:id="@+id/button_druide"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
    android:onClick="showFragment"
        android:text="@string/sorts_de_druide" />

<!-- other buttons -->
</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.