您的位置:首页 > 移动开发 > Android开发

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

2016-04-25 13:19 483 查看

第一章、活用布局

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文件:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="pref_first_preferencescreen_key"
android:title="Preferences">

<PreferenceCategory
android:title="User">

<EditTextPreference
android:key="pref_username"
android:summary="Username"
android:title="Username"/>
</PreferenceCategory>

<PreferenceCategory
android:title="Application">

<Preference
android:key="pref_rate"
android:summary="Rate the app in the store!"
android:title="Rate the app"/>

<Preference
android:key="pref_share"
android:summary="Share the app with your friends"
android:title="Share it"/>

<com.manning.androidhacks.hack004.preference.EmailDialog
android:dialogIcon="@drawable/ic_launcher"
android:dialogTitle="Send Feedback"
android:dialogMessage="Do you want to send an email?"
android:key="pref_sendemail_key"
android:negativeButtonText="Cancel"
android:positiveButtonText="OK"
android:summary="Send your feedback by e-mail"
android:title="Send Feedback"/>

<com.manning.androidhacks.hack004.preference.AboutDialog
android:dialogIcon="@drawable/ic_launcher"
android:dialogTitle="About"
android:key="pref_about_key"
android:negativeButtonText="@null"
android:title="About"/>

</PreferenceCategory>

</PreferenceScreen>


  Activity代码:

public class MainActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
...
Preference ratePref = findPreference("pref_rate");
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
ratePref.setIntent(goToMarket);
}

@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}

@override
protected void onPause() {
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
if (key.equals("pref_username")) {
updateUserText();
}
}

private void updateUserText() {
EditTextPreference pref;
pref = (EditTextPreference) findPreference("pref_username");
String user = pref.getText();

if (user == null)
{
user = "?";
}
pref.setSummary(String.format("Username: %s", user));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: