Skip to content
This repository was archived by the owner on Sep 9, 2022. It is now read-only.

Commit 87cde41

Browse files
committed
Optimized three-day weather display.
1 parent 2ead247 commit 87cde41

File tree

3 files changed

+28
-32
lines changed

3 files changed

+28
-32
lines changed

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 28
4+
compileSdkVersion 29
55
defaultConfig {
66
applicationId "com.absinthe.chillweather"
77
minSdkVersion 21
8-
targetSdkVersion 28
9-
versionCode 662
8+
targetSdkVersion 29
9+
versionCode 677
1010
versionName "1.1.1"
1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1212
}

app/src/main/java/com/absinthe/chillweather/WeatherActivity.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.content.SharedPreferences;
1010
import android.graphics.Color;
1111
import android.graphics.Typeface;
12+
import android.graphics.drawable.Drawable;
1213
import android.net.Uri;
1314
import android.os.Build;
1415
import android.preference.PreferenceManager;
@@ -287,7 +288,9 @@ public void initView() {
287288
mChannel.setShowBadge(false);
288289
mChannel.setVibrationPattern(new long[]{0});
289290
mChannel.setSound(null, null);
290-
manager.createNotificationChannel(mChannel);
291+
if (manager != null) {
292+
manager.createNotificationChannel(mChannel);
293+
}
291294
}
292295

293296
swipeRefresh.setOnRefreshListener(() -> requestWeather(mWeatherId));
@@ -358,20 +361,28 @@ private void showWeatherInfo(Weather weather) {
358361
feelDegreeText.setTypeface(typeface);
359362

360363
forecastLayout.removeAllViews();
364+
365+
int iter = 0;
366+
String[] date = {"今天", "明天", "后天"};
367+
361368
for (Forecast forecast : weather.forecastList) {
362369
View view = LayoutInflater.from(this)
363370
.inflate(R.layout.forecast_item, forecastLayout, false);
364-
TextView dateText = view.findViewById(R.id.tv_forecast_date);
365-
TextView infoText = view.findViewById(R.id.tv_forecast_info);
371+
TextView dateAndConditionText = view.findViewById(R.id.tv_forecast_date_and_info);
366372
TextView maxMinText = view.findViewById(R.id.tv_max_min_degree);
367-
ImageView weatherIcon = view.findViewById(R.id.iv_weather_icon);
368373

369-
dateText.setText(Integer.valueOf(forecast.date.substring(5, 7)) + "月" + Integer.valueOf(forecast.date.substring(8, 10)) + "日");
370-
infoText.setText(forecast.dayCondition);
374+
dateAndConditionText.setText(date[iter++] + "-" + forecast.dayCondition);
371375
maxMinText.setText(forecast.temperatureMax + "℃" + " / " + forecast.temperatureMin + "℃");
372-
weatherIcon.setImageResource(Utility.WeatherIconSelector(forecast.dayCondition, Calendar.getInstance().get(Calendar.HOUR_OF_DAY)));
373376

374-
dateText.setTypeface(typeface);
377+
Drawable image = getResources().getDrawable( Utility.WeatherIconSelector(forecast.dayCondition, Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) );
378+
int h = maxMinText.getLineHeight();
379+
int w = maxMinText.getLineHeight();
380+
381+
image.setBounds( 0, 0, h, w );
382+
maxMinText.setCompoundDrawables(null , null, image, null );
383+
maxMinText.setCompoundDrawablePadding(10);
384+
385+
dateAndConditionText.setTypeface(typeface);
375386
maxMinText.setTypeface(typeface);
376387

377388
forecastLayout.addView(view);

app/src/main/res/layout/forecast_item.xml

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,26 @@
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
android:layout_width="match_parent"
55
android:layout_height="wrap_content"
6+
android:orientation="horizontal"
67
android:layout_margin="15dp">
78

89
<TextView
9-
android:id="@+id/tv_forecast_date"
10+
android:id="@+id/tv_forecast_date_and_info"
1011
android:layout_width="0dp"
1112
android:layout_height="wrap_content"
12-
android:layout_gravity="center_vertical"
13-
android:layout_weight="3"
14-
android:textSize="@dimen/forecast_text_size"
15-
android:textColor="@color/itemTextColor"/>
16-
17-
<TextView
18-
android:id="@+id/tv_forecast_info"
19-
android:layout_width="0dp"
20-
android:layout_height="wrap_content"
21-
android:layout_gravity="center_vertical"
13+
android:layout_gravity="start"
14+
android:gravity="start"
2215
android:layout_weight="1"
23-
android:gravity="center"
2416
android:textSize="@dimen/forecast_text_size"
2517
android:textColor="@color/itemTextColor"/>
2618

27-
<ImageView
28-
android:id="@+id/iv_weather_icon"
29-
android:layout_width="0dp"
30-
android:layout_height="match_parent"
31-
android:layout_gravity="center"
32-
android:layout_weight="1"
33-
android:gravity="center" />
3419

3520
<TextView
3621
android:id="@+id/tv_max_min_degree"
3722
android:layout_width="0dp"
23+
android:layout_weight="1"
3824
android:layout_height="wrap_content"
39-
android:layout_gravity="center"
40-
android:layout_weight="3"
25+
android:layout_gravity="end"
4126
android:gravity="end"
4227
android:textSize="@dimen/forecast_text_size"
4328
android:textColor="@color/itemTextColor"/>

0 commit comments

Comments
 (0)