如何将我的旋转器(下拉)放在Kotlin的Webview顶部?

发布于 2025-01-25 11:07:32 字数 3619 浏览 5 评论 0原文

我是Android开发的新手。 我想我有点基本知识,但是经过8个工作日,我不在网络浏览量上的下拉台上,我想问。

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val languages = resources.getStringArray(R.array.array1)

     val webview = WebView(this)
    setContentView(webview)
     webview.settings.javaScriptEnabled = true
   webview.loadUrl("http://localhost/test/neu/index.php")


    // access the spinner
    val spinner = findViewById<Spinner>(R.id.spinner1)
    if (spinner != null) {
        val adapter = ArrayAdapter(this,
            android.R.layout.simple_spinner_item, languages)
        spinner.adapter = adapter

        spinner.onItemSelectedListener = object :
            AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>,
                                        view: View, position: Int, id: Long) {
                Toast.makeText(this@MainActivity,
                    getString(R.string.selected_item) + " " +
                            "" + languages[position], Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // write code to perform some action
            }
        }
    }
}

它可以做应该做的事情,唯一的事情是将旋转器置于网络视图之下。如果我在ongreate()内评论5-8行,它向我展示了一个只有旋转器的白色屏幕。

寻找这个问题的答案只是为我带来了有关如何制作进度栏的线索,但我没有得到,但是w/e。 在此先感谢

我的清单:xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ttermscannerandroid51">
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.TTermScannerAndroid51">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

最后我的activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/WebView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.377"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:spinnerMode="dropdown" />
    </WebView>
</androidx.constraintlayout.widget.ConstraintLayout>

im new to Android development.
I think i got kinda the basics, but after 8 workhours of not getting my DropDown above my WebView, i tought i gotta ask.

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val languages = resources.getStringArray(R.array.array1)

     val webview = WebView(this)
    setContentView(webview)
     webview.settings.javaScriptEnabled = true
   webview.loadUrl("http://localhost/test/neu/index.php")


    // access the spinner
    val spinner = findViewById<Spinner>(R.id.spinner1)
    if (spinner != null) {
        val adapter = ArrayAdapter(this,
            android.R.layout.simple_spinner_item, languages)
        spinner.adapter = adapter

        spinner.onItemSelectedListener = object :
            AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>,
                                        view: View, position: Int, id: Long) {
                Toast.makeText(this@MainActivity,
                    getString(R.string.selected_item) + " " +
                            "" + languages[position], Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // write code to perform some action
            }
        }
    }
}

It does what it's supposed to do, the only thing is it places the Spinner below the WebView. If i comment out line 5-8 inside the onCreate(), it shows me a white screen with only the Spinner.

Looking for Answers to this question just brings me SO threads on how to make a Progress bar, which i dont get, but w/e.
Thanks in advance

My Manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ttermscannerandroid51">
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.TTermScannerAndroid51">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

at last my activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/WebView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.377"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:spinnerMode="dropdown" />
    </WebView>
</androidx.constraintlayout.widget.ConstraintLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

旧城空念 2025-02-01 11:07:32
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner1" />

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner1" />

</androidx.constraintlayout.widget.ConstraintLayout>
梦太阳 2025-02-01 11:07:32

不要在WebView中嵌套旋转器,只需给予它的约束,以便它位于所需的位置。

我不确定您是否是指在上垂直> em> em> em> 叠加 - 如果是上面的,则将旋转器的顶部限制为父母的顶部, WebView的顶部到旋转器的底部:

<Spinner
    android:id="@+id/spinner1"
    ...
    app:layout_constraintTop_toTopOf="parent"/>

<WebView
    android:id="@+id/WebView"
    ...
    app:layout_constraintTop_toBottomOf="@id/spinner1"/>

如果要覆盖它,请将旋转器的顶部限制为WebView的顶部,等等:

<WebView
    android:id="@+id/WebView"
    ...
    app:layout_constraintTop_toTopOf="parent"/>

<Spinner
    android:id="@+id/spinner1"
    ...
    app:layout_constraintTop_toTopOf="@id/WebView"/>

现在,Spinner与WebView相对,并且可以使用边距,约束,等于将其相对于WebView的边缘定位。叠加时,订单很重要,但是您可以给它一个hevation值,以明确地将其移动更高,因此它可以通过WebView显示

Don't nest your Spinner inside the Webview, just give it constraints so it sits where you want.

I'm not sure if you mean you want it vertically above the WebView, or overlaid - if it's above, constrain the top of the spinner to the top of the parent, and the top of the webview to the bottom of the spinner:

<Spinner
    android:id="@+id/spinner1"
    ...
    app:layout_constraintTop_toTopOf="parent"/>

<WebView
    android:id="@+id/WebView"
    ...
    app:layout_constraintTop_toBottomOf="@id/spinner1"/>

If you want it overlaid, constrain the top of the spinner to the top of the webview, etc:

<WebView
    android:id="@+id/WebView"
    ...
    app:layout_constraintTop_toTopOf="parent"/>

<Spinner
    android:id="@+id/spinner1"
    ...
    app:layout_constraintTop_toTopOf="@id/WebView"/>

Now the spinner is relative to the WebView, and you can use margins, constraints etc to position it relative to the WebView's edges. When overlaying, order matters, but you can give it an elevation value to explicitly move it higher, so it displays over the WebView

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