在具有滚动视图的Android响应式设计屏幕中使其响应?

时间:2018-12-15 06:14:17

标签: android xml

Image 我是Android新手,所以我不知道如何

使用

设计此屏幕以图像形式显示

滚动视图并使之响应

请给我个想法或例子

2 个答案:

答案 0 :(得分:0)

根据您的要求,我为您提供了基本的解决方案,您可以更改图像图标,边距以及布局的高度和宽度,等等。

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginVertical="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="28dp"
                        android:layout_height="28dp"
                        android:padding="2dp"
                        android:gravity="center"
                        android:background="@drawable/circle"
                        android:text="1" />

                    <ImageView
                        android:id="@+id/img1"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:layout_centerInParent="true"
                        android:background="@drawable/ic_fingerprint_black_24dp" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:gravity="center"
                        android:layout_below="@+id/img1"
                        android:layout_marginTop="10dp"
                        android:text="hello" />

                </RelativeLayout>

                <View
                    android:layout_width="5dp"
                    android:layout_marginVertical="15dp"
                    android:background="@color/colorPrimary"
                    android:layout_height="80dp" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="28dp"
                        android:layout_height="28dp"
                        android:padding="2dp"
                        android:layout_marginLeft="5dp"
                        android:gravity="center"
                        android:background="@drawable/circle"
                        android:text="8" />

                    <ImageView
                        android:id="@+id/img2"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:layout_centerInParent="true"
                        android:background="@drawable/ic_fingerprint_black_24dp" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/img2"
                        android:layout_centerHorizontal="true"
                        android:gravity="center"
                        android:layout_marginTop="10dp"
                        android:text="hello" />

                </RelativeLayout>

            </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <View
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_marginVertical="10dp"
                android:background="@color/colorPrimary"
                android:layout_marginHorizontal="15dp"
                android:layout_height="5dp" />

            <View
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_marginHorizontal="15dp"
                android:layout_marginVertical="10dp"
                android:background="@color/colorPrimary"
                android:layout_height="5dp" />

        </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginVertical="10dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="28dp"
                        android:layout_height="28dp"
                        android:padding="2dp"
                        android:gravity="center"
                        android:background="@drawable/circle"
                        android:text="1" />

                    <ImageView
                        android:id="@+id/img11"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:layout_centerInParent="true"
                        android:background="@drawable/ic_fingerprint_black_24dp" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/img11"
                        android:layout_centerHorizontal="true"
                        android:gravity="center"
                        android:layout_marginTop="10dp"
                        android:text="hello" />

                </RelativeLayout>

                <View
                    android:layout_width="5dp"
                    android:layout_marginVertical="15dp"
                    android:background="@color/colorPrimary"
                    android:layout_height="80dp" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="28dp"
                        android:layout_height="28dp"
                        android:padding="2dp"
                        android:layout_marginLeft="5dp"
                        android:gravity="center"
                        android:background="@drawable/circle"
                        android:text="8" />

                    <ImageView
                        android:id="@+id/img21"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:layout_centerInParent="true"
                        android:background="@drawable/ic_fingerprint_black_24dp" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/img21"
                        android:layout_centerHorizontal="true"
                        android:gravity="center"
                        android:layout_marginTop="10dp"
                        android:text="hello" />

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <View
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:layout_marginVertical="10dp"
                    android:background="@color/colorPrimary"
                    android:layout_marginHorizontal="15dp"
                    android:layout_height="5dp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:layout_marginHorizontal="15dp"
                    android:layout_marginVertical="10dp"
                    android:background="@color/colorPrimary"
                    android:layout_height="5dp" />

            </LinearLayout>

        </LinearLayout>
    </ScrollView>
</LinearLayout>

答案 1 :(得分:0)

API 30

对宽度、高度和边距使用计算值

ex - height = (getHeight(this) / 0.5) //设备高度的 50%

获取设备宽度和高度

    fun getWidth(context: Context): Int {
        val displayMetrics = DisplayMetrics()
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            context.display?.getRealMetrics(displayMetrics)
            displayMetrics.widthPixels
        }else{
            this.windowManager.defaultDisplay.getMetrics(displayMetrics)
            displayMetrics.widthPixels
        }
    }

    fun getHeight(context: Context): Int {
        val displayMetrics = DisplayMetrics()
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            context.display?.getRealMetrics(displayMetrics)
            displayMetrics.heightPixels
        }else {
            val displayMetrics = DisplayMetrics()
            this.windowManager.defaultDisplay.getMetrics(displayMetrics)
            displayMetrics.heightPixels
        }
    }

MainActiviy onCreate() 方法

为视图动态设置值,因此它们会根据设备大小进行缩放

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

        val view = findViewById<Button>(R.id.button).layoutParams as ViewGroup.MarginLayoutParams
        view.apply {
            width = (getWidth(this@MainActivity) * 0.5).toInt()
            height = (getHeight(this@MainActivity) * 0.5).toInt() //scroll view height should maupulate by it's children, for dynamic layouts
            setMargins(50, 50, 0, 50)
        }
    }

包含滚动视图的布局 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4CAF50"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#C81D1D">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

设计

enter image description here

设备上的实际结果

enter image description here

<块引用>

如果要减去操作栏高度和状态栏高度来得到 实际可用的视图高度,使用下面的代码

height = (getHeight(this@MainActivity) * 0.5).toInt()  - getActionBarHeight() - getStatusBarHeight()

操作栏高度和状态栏高度

fun getStatusBarHeight(): Int {
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId)
        }
        return 0
    }

    fun getActionBarHeight() : Int {
        val tv = TypedValue()
        this.theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)
        return resources.getDimensionPixelSize(tv.resourceId)
    }

结果

enter image description here