java - Android Studio 中的响应式 View

标签 java android xml

friend 们,我正在 Android Studio 上开发一个应用程序,该应用程序将在平板电脑和其他 Android 手机上使用。但我在设计 View (模板)时遇到一些问题。请帮我解决这个问题。

我使用 ConstraintLayout 作为 mainLayout,在其中我使用垂直方向的线性布局。您可以检查代码和输出

如果我使用 listView highet 作为 0dp 那么它不会显示任何内容

  <?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:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/my_images_gallary"
                android:textSize="36sp"
                android:textStyle="bold" />

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="0dp">

            </ListView>

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:text="Button" />
        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
[Output for the height is 0dp][1]
and if i use the ListView Height as match_parent so the bottom button will not appear.

<?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:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/my_images_gallary"
            android:textSize="36sp"
            android:textStyle="bold" />

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </ListView>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:text="Button" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Output for the height is match_parent 实际上我希望 listView 应该显示所有滚动内容,并且无论 listview 是否滚动,按钮都应该始终可见。 Expected Output which i am failed to get

最佳答案

想法1:

give fixed height to button and textview. Then use 0dp height to listview

想法2:

Use weight_sum to linear layout and give layout_weight to button,textview and listview as you expect.

关于java - Android Studio 中的响应式 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57973212/

相关文章:

python - 为什么 lxml.etree.iterparse() 会占用我所有的内存?

xml - 如何使用 XML::XPath 获取属性?

多行 Java 标签

java - 无法创建 Jena TDB 的第一个实例

android - onReceive() android.net.ConnectivityManager.CONNECTIVITY_ACTION

Android appwidget 在运行时不启动服务

android 输入表单上的 java.lang.ArrayIndexOutOfBoundsException

xml - 使用 XSLT 对简单 XML 应用 Muenchian 分组

java - Struts2 - 将重定向设置为 false

java - 如何向从 Eclipse 启动的 Java 程序添加命令行参数?