Android开发之50个常见实用技巧——活用布局

第一章、活用布局

Hack1. 使用weight属性实现视图的居中显示

   ①合用weightSum属性和layout-weight属性

    解决问题,如:居中显示按钮,并占据父视图的50%;

      代码如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:backgorund="#FFFFFF"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="click me"/>
</LinearLayout>

 

   分析:

     指定LinearLayout的android:weightSum属性值为1,表示内部所有子视图的weight总和是1;  

  参考链接: http://developer.android.com/reference/android/widget/LinearLayout.html

 

 

Hack2 使用延迟加载以及避免代码重复

   ①使用<include/>标签避免代码重复

      如:<include layout="@layout/已经写好的布局文件命"/>

   ②通过viewStub实现view的延迟加载 

      ViewStub 是一种不可视并且大小为 0 的视图,可以延迟到运行时填充(inflate)布局资源。

    当 ViewStub 设置为可视或者inflate() 方法被调用后,就会填充布局资源,然后 ViewStub 便会

    被填充的视图替代。

  inflatedId 属性:是调用viewStub的inflate()方法或setVisibility()方法时返回的ID (被填充的View的ID);

  只需调用setVisibility(View.VISIBLE) 方法即可加载 显示在layout中引用的布局 map

     如: android:layout="@layout/map".

 

Hack3 创建定制的ViewGroup

  情景分析:

    需要在不同的Activity中显示复杂的视图

  优点:

    1、在同的Activity中复用该视图时,更易维护

    2、开发者可以使用自定义属性来定制ViewGroup中子视图的位置

    3、布局文件更简明,更容易理解

    4、如果需要修改margin,不必重新手写计算机每个子视图的margin

   ①理解Android绘制视图的方式

    绘制布局由两个遍历过程组成: 测量过程和布局过程

   ②创建CascadeLayout类,继承自ViewGroup

    重新建attrs.xml文件用于定义那些定制的属性

    在res/values下,建dimens.xml文件,用于保存指定水平间距和垂直间距的默认值

    重写ViewGroup的OnMeasure()、 OnLayout()方法

    ③为子视图添加自定义属性

 

Hack4 偏好设置使用技巧

  偏好设置框架(Preference)的目的是创建简单的偏好设置界面,如果想添加更多复杂UI控制或逻辑,建议单独

创建一个Activity并使用Dialog的主题,然后从偏好设置控件上启动它。

  XML文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <PreferenceScreen
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:key="pref_first_preferencescreen_key"
 5     android:title="Preferences">
 6 
 7     <PreferenceCategory
 8         android:title="User">
 9         
10         <EditTextPreference
11             android:key="pref_username"
12             android:summary="Username"
13             android:title="Username"/>
14     </PreferenceCategory>
15     
16     <PreferenceCategory
17         android:title="Application">
18     
19         <Preference
20             android:key="pref_rate"
21             android:summary="Rate the app in the store!"
22             android:title="Rate the app"/>
23     
24         <Preference
25             android:key="pref_share"
26             android:summary="Share the app with your friends"
27             android:title="Share it"/>
28 
29         <com.manning.androidhacks.hack004.preference.EmailDialog
30             android:dialogIcon="@drawable/ic_launcher"
31             android:dialogTitle="Send Feedback"
32             android:dialogMessage="Do you want to send an email?"
33             android:key="pref_sendemail_key"
34             android:negativeButtonText="Cancel"
35             android:positiveButtonText="OK"
36             android:summary="Send your feedback by e-mail"
37             android:title="Send Feedback"/>
38         
39         <com.manning.androidhacks.hack004.preference.AboutDialog
40             android:dialogIcon="@drawable/ic_launcher"
41             android:dialogTitle="About"
42             android:key="pref_about_key"
43             android:negativeButtonText="@null"
44             android:title="About"/>
45 
46     </PreferenceCategory>
47 
48 </PreferenceScreen>

  Activity代码:

 1 public class MainActivity extends PreferenceActivity implements
 2     OnSharedPreferenceChangeListener {
 3         @Override
 4         public void onCreate(Bundle savedInstanceState) {
 5             super.onCreate(savedInstanceState);
 6             addPreferencesFromResource(R.xml.prefs);
 7             ...
 8             Preference ratePref = findPreference("pref_rate");
 9             Uri uri = Uri.parse("market://details?id=" + getPackageName());
10             Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
11             ratePref.setIntent(goToMarket);
12         }
13 
14         @Override
15         protected void onResume() {
16             super.onResume();
17             getPreferenceScreen().getSharedPreferences()
18                 .registerOnSharedPreferenceChangeListener(this);
19         }
20 
21         @override
22         protected void onPause() {
23             getPreferenceScreen().getSharedPreferences()
24                 .unregisterOnSharedPreferenceChangeListener(this);
25         }
26 
27         @Override
28         public void onSharedPreferenceChanged(
29             SharedPreferences sharedPreferences, String key) {
30                 if (key.equals("pref_username")) {
31                     updateUserText();
32                 }
33         }
34 
35         private void updateUserText() {
36             EditTextPreference pref;
37             pref = (EditTextPreference) findPreference("pref_username");
38             String user = pref.getText();
39 
40             if (user == null)
41             {
42                 user = "?";
43             }
44             pref.setSummary(String.format("Username: %s", user));
45         }
46 }

 

posted @ 2016-04-25 13:19  AnglusWang  阅读(324)  评论(0编辑  收藏  举报