首页 > 解决方案 > 我在全屏 Android 应用程序中定位视图时遇到问题

问题描述

嘿,非常感谢你的帮助,代码看起来很棒,但现在我遇到了各种各样的问题......首先我不能用第二个堆叠的 ImageView 做一个圆圈,我也不能将前 3 个 ImageView 定位到具有 8dp 填充的屏幕顶部,如果整个事物变成一个大集群,它实际上不会让我做任何事情。我有一个我想要实现的图像,并将附上它。如果大家有什么想法欢迎大家。顺便非常感谢您!在此处输入图像描述

标签: androidandroid-layout

解决方案


XML 代码

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout 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"
                android:background="?attr/fullscreenBackgroundColor"
                android:theme="@style/ThemeOverlay.Experiment.FullscreenContainer"
                tools:context=".FullscreenActivity">
            
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
            
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:padding="8dp">
            
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="4dp"
                            android:layout_weight="1"
                            android:src="@mipmap/ic_launcher"
                            android:textAllCaps="false"
                            android:textColor="@color/white" />
            
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="4dp"
                            android:layout_weight="1"
                            android:src="@mipmap/ic_launcher"
                            android:textAllCaps="false"
                            android:textColor="@color/white" />
            
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="4dp"
                            android:layout_weight="1"
                            android:src="@mipmap/ic_launcher"
                            android:textAllCaps="false"
                            android:textColor="@color/white" />
                    </LinearLayout>
            
                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
            
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:layout_margin="12dp"
                            android:background="@color/white"
                            android:src="@drawable/ic_launcher_background"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
            
                        <de.hdodenhof.circleimageview.CircleImageView
                            android:id="@+id/profile_image"
                            android:layout_width="120dp"
                            android:layout_height="120dp"
                            android:src="@drawable/cybersecurity_1134x638"
                            app:civ_border_color="#FF000000"
                            app:civ_border_width="2dp"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0.526"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
            
                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="4dp"
                            android:backgroundTint="@color/white"
                            android:text="Inner Button"
                            android:textAllCaps="false"
                            android:textColor="@color/black"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0.762"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintVertical_bias="0.795" />
                    </androidx.constraintlayout.widget.ConstraintLayout>
            
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="4dp"
                        android:backgroundTint="@color/black"
                        android:text="Button1"
                        android:textAllCaps="false"
                        android:textColor="@color/white" />
            
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="4dp"
                        android:backgroundTint="@color/black"
                        android:text="Button2"
                        android:textAllCaps="false"
                        android:textColor="@color/white" />
            
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="4dp"
                        android:backgroundTint="@color/black"
                        android:text="Button3"
                        android:textAllCaps="false"
                        android:textColor="@color/white" />
            
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="4dp"
                        android:backgroundTint="@color/black"
                        android:text="Button4"
                        android:textAllCaps="false"
                        android:textColor="@color/white" />
            
                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="4dp"
                        android:backgroundTint="@color/black"
                        android:text="Button5"
                        android:textAllCaps="false"
                        android:textColor="@color/white" />
            
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:orientation="horizontal">
            
                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="4dp"
                            android:backgroundTint="@color/white"
                            android:text="Button"
                            android:textAllCaps="false"
                            android:textColor="@color/black" />
            
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_margin="12dp"
                            android:background="@color/black"
                            android:src="@drawable/ic_launcher_background" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

设计输出

在此处输入图像描述

https://github.com/hdodenhof/CircleImageView用于圆形 ImageView。

  1. 在您的应用级别 gradle 文件中添加依赖项。

    实施 'de.hdodenhof:circleimageview:3.1.0'

  2. 将 ImageView 添加或替换到您的 XML 文件中。

             <de.hdodenhof.circleimageview.CircleImageView
             android:id="@+id/profile_image"
             android:layout_width="120dp"
             android:layout_height="120dp"
             android:src="@drawable/cybersecurity_1134x638"
             app:civ_border_color="#FF000000"
             app:civ_border_width="2dp"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintHorizontal_bias="0.526"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent" />
    

对于热门图片,我认为您应该检查 imageview 的高度而不是 wrap_content 您可以使用静态值进行更改。


推荐阅读