
material-ui
這篇文章介紹了如何使用材料設計指南來建立天氣應用。 材質設計是一組視覺設計,UI互動,動作等規則。 這些規則可幫助開發人員設計和建立Android應用。
這篇文章想描述我們如何使用Weatherlib作為天氣層和Material設計規則來建立天氣應用。 我們不僅要為原生支援Material Design的Android 5 Lollipop開發此應用,還希望支援4.x Kitkat之類的Android早期版本。 是以,我們将引入appCompat v7庫,即使在先前的Android版本中,該庫也可以幫助我們實作Material Design。
我們想編寫一個具有擴充工具欄的應用程式,其中包含有關位置和目前天氣的一些資訊以及有關溫度,天氣圖示,濕度,風和壓力的一些基本天氣資訊。 最後,我們将得到如下圖所示的圖檔:
Android項目設定
我們要做的第一件事是配置項目,以便我們可以使用Weatherlib,尤其是appCompat v7。 我們可以打開build.graddle并添加以下行:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.mcxiaoke.volley:library:[email protected]'
compile 'com.survivingwithandroid:weatherlib:1.5.3'
compile 'com.survivingwithandroid:weatherlib_volleyclient:1.5.3'
}
現在我們已經正确設定了項目,我們可以開始定義應用布局。
應用布局:材質設計
如前所述,我們想在擴充工具欄上使用工具欄。 工具欄是操作欄的綜合,可以給我們更多的控制權。 與緊密綁定到Actvitiy的操作欄不同,工具欄可以放置在View層次結構内的任何位置。
是以,我們的布局将分為三個主要區域:
- 工具欄區域
- 天氣圖示和溫度
- 其他天氣資料
布局如下圖所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WeatherActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_height="128dp"
app:popupTheme="@style/ActionBarPopupThemeOverlay"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:paddingLeft="72dp"
android:paddingBottom="16dp"
android:gravity="bottom"
app:titleTextAppearance="@style/Toolbartitle"
app:subtitleTextAppearance="@style/ToolbarSubtitle"
app:theme="@style/ThemeOverlay.AppCompat.Light"
android:title="@string/location_placeholder"
/>
....
</RelativeLayout>
如您所見,我們使用了工具欄。 如指南中所述,我們将工具欄的高度設定為等于128dp,此外,我們使用原色作為背景。 原色在colors.xml中定義。 您可以參考材料設計顔色準則以擷取更多資訊。 我們應該定義至少三種不同的顔色:
- 原色,由500辨別
- 700辨別的主要深色
- 應當是主要操作按鈕等的強調顔色
工具欄的背景色設定為原色。
<resources>
<color name="primaryColor_500">#03a9f4</color>
<color name="primaryDarkColor_700">#0288d1</color>
....
</resources>
此外,工具欄内的左側填充和底部填充是根據準則定義的。 最後,我們添加菜單項,就像使用操作欄一樣。 主要結果如下所示:
如您所見,工具欄的背景等于原色。
搜尋城市:帶有材料設計的彈出視窗
我們可以使用彈出視窗讓使用者輸入位置。 彈出視窗非常簡單,它由一個用于輸入資料的EditText和一個簡單的ListView組成,該ListView顯示與插入EditText中的模式比對的城市。 我不會介紹如何在weatherlib中搜尋城市,因為我已經介紹了它。 結果顯示在這裡:
彈出布局如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dialog.city.header"
style="@style/Theme.AppCompat.Dialog"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8sp"
android:text="@string/dialog.city.pattern"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="@+id/ptnEdit"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cityList"
android:clickable="true"/>
</LinearLayout>
建立和處理對話框的代碼如下所示:
private Dialog createDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.select_city_dialog, null);
builder.setView(v);
EditText et = (EditText) v.findViewById(R.id.ptnEdit);
....
et.addTextChangedListener(new TextWatcher() {
....
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count > 3) {
// We start searching
weatherclient.searchCity(s.toString(), new WeatherClient.CityEventListener() {
@Override
public void onCityListRetrieved(List<City> cities) {
CityAdapter ca = new CityAdapter(WeatherActivity.this, cities);
cityListView.setAdapter(ca);
}
});
}
}
});
builder.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// We update toolbar
toolbar.setTitle(currentCity.getName() + "," + currentCity.getCountry());
// Start getting weather
getWeather();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
需要注意的重要一點是在第33行,根據使用者選擇的城市設定工具欄标題,然後擷取目前天氣。 這段代碼的結果如下所示:
天氣庫:天氣
要擷取目前天氣,我們使用Weatherlib:
private void getWeather() {
weatherclient.getCurrentCondition(new WeatherRequest(currentCity.getId()),
new WeatherClient.WeatherEventListener() {
@Override
public void onWeatherRetrieved(CurrentWeather currentWeather) {
// We have the current weather now
// Update subtitle toolbar
toolbar.setSubtitle(currentWeather.weather.currentCondition.getDescr());
tempView.setText(String.format("%.0f",currentWeather.weather.temperature.getTemp()));
pressView.setText(String.valueOf(currentWeather.weather.currentCondition.getPressure()));
windView.setText(String.valueOf(currentWeather.weather.wind.getSpeed()));
humView.setText(String.valueOf(currentWeather.weather.currentCondition.getHumidity()));
weatherIcon.setImageResource(WeatherIconMapper.getWeatherResource(currentWeather.weather.currentCondition.getIcon(), currentWeather.weather.currentCondition.getWeatherId()));
setToolbarColor(currentWeather.weather.temperature.getTemp());
}
....
}
您會注意到,在第8行,我們根據目前天氣設定工具欄字幕,而在第15行,我們根據目前溫度更改工具欄顔色。 作為工具欄的背景色,我們使用了準則中顯示的原色。
- 源代碼可從@github獲得。
翻譯自: https://www.javacodegeeks.com/2014/11/develop-android-weather-app-with-material-design.html
material-ui