首页 > 解决方案 > 如何设计微调器的这个 XML 文件

问题描述

在这里,我附上了设计的屏幕截图,请帮助我这是一个项目,我是 android 的新手在此处输入图像描述

标签: xmlandroid-layoutlayoutmaterial-designandroid-spinner

解决方案


试试这个代码:

首先创建一个可绘制文件

spinner_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <corners android:radius="5dp" />
        <solid android:color="#F5F5F5"/>
        <stroke android:width="3dp"
            android:color="#454551"/>
    </shape>
</item>
</selector>

现在,使用spinner_bg.xml布局文件作为背景,布局文件的代码如下:

main_activity.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"
tools:context=".MainActivity"
android:background="#F5F5F5"
android:orientation="vertical">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/spinner_bg"
        android:layout_marginTop="15dp">
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="50dp"/>
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product Group"
        android:textSize="18sp"
        android:background="#F5F5F5"
        android:layout_marginLeft="10dp"
        android:padding="5dp"/>
</RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/spinner_bg"
        android:layout_marginTop="15dp">
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="50dp"/>
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Literature"
        android:textSize="18sp"
        android:background="#F5F5F5"
        android:layout_marginLeft="10dp"
        android:padding="5dp"/>
</RelativeLayout>
</LinearLayout>

这是上述代码的输出:

在此处输入图像描述

在此处输入图像描述

我希望它对你有用。


推荐阅读