【发布时间】:2021-08-04 04:21:54
【问题描述】:
我正在为我的学习开发一个项目,但我犯了一个错误:我只是为我的手机(我使用的是 Pixel 4 XL 型号)和类似产品开发的。
但是,当我在较小的设备(如 Pixel 3)上运行它时,布局非常糟糕。我没有使用约束布局,因为我真的很讨厌它,所以我在整个应用程序中都使用了线性布局。
我不关心平板电脑,只想让它在小型设备(至少 5 英寸)上工作。我读过一些关于创建新布局文件夹的内容,比如 layout-swXXX 但我不确定,我想听听您对最佳方式的看法。
这是我的项目树:
我的布局示例:
<?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="@drawable/background"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="43dp"
android:layout_height="53dp"
app:tint="@color/DarkRed"
app:srcCompat="@drawable/ic_gps" />
<TextView
android:id="@+id/lastLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:text="@string/last_position"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/textLocation"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/LightGrey"
android:text="@string/no_position_found" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/ic_map"
android:layout_width="35dp"
android:layout_height="35dp"
app:srcCompat="@drawable/ic_map" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp">
<androidx.cardview.widget.CardView
android:id="@+id/card_compile_module"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginHorizontal="20dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="#4a4747"
app:cardCornerRadius="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView3"
android:layout_width="64dp"
android:layout_height="64dp"
app:srcCompat="@drawable/ic_module"
app:tint="#3acf9f" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:maxLines="2"
android:text="@string/compile"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_download"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginHorizontal="20dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="#4a4747"
app:cardCornerRadius="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="64dp"
android:layout_height="64dp"
app:srcCompat="@drawable/ic_download_file"
app:tint="#3acf9f" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:maxLines="2"
android:text="@string/download"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp">>
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:id="@+id/card_profile"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginHorizontal="20dp"
app:cardBackgroundColor="#4a4747"
app:cardCornerRadius="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView5"
android:layout_width="64dp"
android:layout_height="64dp"
app:srcCompat="@drawable/ic_user"
app:tint="#3acf9f" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/user_settings"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:id="@+id/card_gps"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginHorizontal="20dp"
app:cardBackgroundColor="#4a4747"
app:cardCornerRadius="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView6"
android:layout_width="64dp"
android:layout_height="64dp"
app:srcCompat="@drawable/ic_gps_settings"
app:tint="#3acf9f" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/gps_settings"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_marginTop="100dp"
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:gravity="center"
android:text="@string/developed_by"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
【问题讨论】:
标签: java xml android-studio android-linearlayout