效果:
第一步:创建项目:
第二步:导入素材
将图片素材导入到drawable文件夹中
第三步:编写布局
打开activity_main.xml文件
<?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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/weather_bg"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="@drawable/icon_user"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="70dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@drawable/baseline_arrow_back_24" />
<!--<LinearLayout
android:id="@+id/ss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="91dp">
<ImageView
android:id="@+id/title_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="@drawable/baseline_arrow_back_24" />
</LinearLayout>-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:importantForAccessibility="no"
android:src="@drawable/baseline_location_on_24"
app:layout_constraintBottom_toBottomOf="@id/dialog_button"
app:layout_constraintStart_toStartOf="@id/dialog_button"
app:layout_constraintTop_toTopOf="@id/dialog_button" />
<com.google.android.material.button.MaterialButton
android:id="@+id/dialog_button"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="北京市"
android:textColor="@color/black"
android:textSize="18sp"
app:backgroundTint="@android:color/transparent"
app:cornerRadius="2dp"
app:icon="@drawable/ic_keyboard_arrow_down_black_24dp"
app:iconGravity="end"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_bar"
app:layout_constraintVertical_bias="0.02"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/view_bg"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:importantForAccessibility="no"
android:src="@drawable/view_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.7"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/dialog_button"
app:layout_constraintVertical_bias="0.4"
app:layout_constraintWidth_percent="0.7" />
<TextView
android:id="@+id/tv_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="34"
android:textColor="@color/black"
android:textSize="120sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@id/view_bg"
app:layout_constraintEnd_toEndOf="@id/view_bg"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/view_bg"
app:layout_constraintTop_toTopOf="@id/view_bg"
app:layout_constraintVertical_bias="0.2" />
<TextView
android:id="@+id/tv_weather_wind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="晴 南风 4级"
android:textColor="#1E1E1A"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_temperature" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/view_bg"
app:layout_constraintEnd_toEndOf="@+id/view_bg"
app:layout_constraintStart_toStartOf="@+id/view_bg"
app:layout_constraintTop_toBottomOf="@+id/tv_weather_wind"
android:layout_margin="220px"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
android:src="@drawable/ic_temper" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="19-28℃"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
android:src="@drawable/ic_humidity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="湿度:65%"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
android:src="@drawable/ic_wind" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="风速:5km/h"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_state"
android:layout_width="188dp"
android:layout_height="152dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_cloudy"
app:layout_constraintBottom_toBottomOf="@id/tv_temperature"
app:layout_constraintEnd_toEndOf="@id/view_bg"
app:layout_constraintStart_toEndOf="@id/tv_temperature"
app:layout_constraintTop_toTopOf="@id/tv_temperature"
app:layout_constraintVertical_bias="0"
app:layout_constraintWidth_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
第四步:编写java代码
创建列表
在res文件夹下创建menu文件夹
在menu文件夹中创建cities_menu.xml文件
<?xml version ="1.0" encoding ="utf-8"?><!-- Learn More about how to use App Actions: https://developer.android.com/guide/actions/index.html -->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/city_beijing"
android:title="北京市" />
<item
android:id="@+id/city_shanghai"
android:title="上海市" />
<item
android:id="@+id/city_shenzhen"
android:title="深圳市" />
</menu>
添加列表信息
进入MainActivity.java文件
package com.example.myapplication;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupMenu;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.button.MaterialButton;
public class MainActivity extends AppCompatActivity {
public MaterialButton materialButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
// 类用不了用第二种方法
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//全屏模式
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LOW_PROFILE);
//点击下拉列表
materialButton = findViewById(R.id.dialog_button);
materialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu(v);
}
});
}
private void showPopupMenu(View view) {
PopupMenu popupMenu = new PopupMenu(this, view);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.cities_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.city_beijing) {
materialButton.setText("北京市");
return true;
} else if (itemId == R.id.city_shanghai) {
materialButton.setText("上海市");
return true;
} else if (itemId == R.id.city_shenzhen) {
materialButton.setText("深圳市");
return true;
}
return false;
}
});
popupMenu.show();
}
}
运行就好啦