Tricks of Android's GUI

*/-->

Tricks of Android's GUI

1 layoutweight

In LinearLayout, the default weight for all views is 0, so if you specify any weight value greater than 0 to only one view, then that view fills whatever space remains after all views are given the space they require. It can be used in screen adaptation.

2 Saving Data Across Rotation

2.1 Problem

We should understand that activity would be destroyed and recreated when the rotation happen. Obviously, so are the data. So you have to do something before you rotate the devices. Also android will never destroy a running activity to reclaim memory, only in the paused or stoped state. So be careful if your activity turn into these states. And remember an activity's onSaveInstanceState(..) method would be called if it's paused or stopped.

2.2 Solu

We can ride onSaveInstanceState(…) to save addtional data to the bundle and then read that data back in onCreate().
#+BEGIN Java
@Over
pc void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
uper.onSaveInstanceState(outState, outPersistentState);
outState.putInt(KEYINDEX, mCurrentIndex);
}
#+ENDSRC
In onCreate(), you can

if(saveInstanceState != null){
//your code
}

3 ActivityManager

4 Fragment

Fragment has to rely on Activity, a fragment is a controller object that can manager a user interface which is a entire screen or a part of the screen.

4.1 Two Approach to Hosting Fragment

One approach is to add fragment into activity's layout, but it's not inflexible. Because you can't swap out the fragment during the activity lifecycle.
Another approach is managered in activity classs. Although it's complicate, but it is flexible and we can handle fragment.

4.2 FragmentManager

Similar with Activity, there is FragmentManager to manager the Fragment. But diffierent with ActivityManager, FragmentManager must be explicit called to handle fragment.

5 dp, px,sp

In MDPI: dp = px = sp
In HDPI: px < dp = sp
In HDPI with larger text: px < dp < sp
Recommendtion: use dp and sp, not px or else.

6 Layout Parameters

Attributes whose names do not begin with layout_ are directions to the widget. When it is inflated, the widget calls a method to configure itself based on each of these attributes and their values.
When an attribute’s name begins with layout_ , that attribute is a direction to that widget’s parent. These attributes are known as layout parameters, and they tell the parent layout how to arrange the child element within the parent.

7 Passing Data Between Two Fragments

There are two Fragments named A and B, if you want to create B in A. You can use B.newInstance() method, and the method carry the data that you want to passing from A to B. Then if there are data return from B, you can call Fragment.setTargeFragment() (similar with Activity.startActivityForResult()) in A, and call getTargetFragment() in B. Then we have a connection between A and B, but we still have to call onActivityResult() in A.

8 AppCompatActivity

8.1 Action Bar

We should note that the res/menu xml files now need to use an app: prefix for showAsAction as shown below:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_refresh"
android:title="@string/action_refresh"
android:icon="@drawable/ic_action_refresh"
app:showAsAction="ifRoom" />
</menu>

You can find more details on https://github.com/codepath/android_guides/wiki/Extended-ActionBar-Guide

8.2 Ancestral Navigation

You should do as shown below:

((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

8.2.1 Call setDisplayHomeAsUpEnabled() in Fragment

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(getActivity(), CrimeListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();
default:
return super.onOptionsItemSelected(item);
}
}

8.2.2 Add Parent Activity metadata

<activity>
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".parentActivity"/>
</activity>

In onOptionsItemSelected()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (NavUtils.getParentActivityName(getActivity()) != null) {
NavUtils.navigateUpFromSameTask(getActivity());
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Before you calling NavUtils,you should make sure the current activity has a parent activity.

onCreateView(){
if(NavUtils.getParentActivityName(getActivity())!=null){
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

This way is better.

9 ListView Empty

If the list is empty, then the listview will be a big void. User can't interact with that GUI, so we should add a view if the list is empty, and remove it when the list isn't empty.

9.1 Solution 1

Using setEmptyView():

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageButton
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="25dp"
android:background="@android:drawable/ic_input_add"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </FrameLayout>

In onCreateView()

View v = inflater.inflate(R.layout.activity_fragment, container, false);
ListView listview = (ListView) v.findViewById(android.R.id.list);
ImageButton imgBtn = (ImageButton) v.findViewById(R.id.empty);
imgBtn.setVisibility(View.GONE);
listview.setEmptyView(imgBtn);
// View v = inflater.inflate(R.layout.activity_fragment, null, false);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Crime crime = new Crime();
CrimeLab.get(getActivity()).addCrime(crime);
Intent intent = new Intent(getActivity(), CrimePagerActivity.class);
intent.putExtra(CrimeFragment.EXTRA_CRIME, crime.getCrimeId());
startActivityForResult(intent, 0);
getActivity().findViewById(R.id.empty).setVisibility(View.INVISIBLE);
}
});

9.2 TODO Solution2

I haven't figured out yet.

Author: mlhy

Created: 2015-10-28 Wed 12:49

Emacs 24.5.1 (Org mode 8.2.10)

Tricks of Android's GUI的更多相关文章

  1. 图解Android - Android GUI 系统 (1) - 概论

    Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...

  2. 国庆大礼包:2014年最全的ANDROID GUI模板和线框图免费下载

    距离上次分享GUI模板有很长时间了,这段时间里设计趋势不断变化,谷歌推出了最新的Android L以及全新的界面设计,UI设计师又有得忙了,今天收集了一组实用的GUI模板和线框图,包含最新的Andro ...

  3. Android Weekly Notes Issue #221

    Android Weekly Issue #221 September 4th, 2016 Android Weekly Issue #221 ARTICLES & TUTORIALS And ...

  4. 浅析 Android 的窗口

    来源:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=555&fromuid=6   一.窗口的概念 在开发过程中,我们经常会 ...

  5. Android 开发之 ---- 底层驱动开发(一) 【转】

    转自:http://blog.csdn.net/jmq_0000/article/details/7372783 版权声明:本文为博主原创文章,未经博主允许不得转载. 驱动概述 说到 Android ...

  6. 游戏引擎/GUI的设计与实现-序

    几年前写<嵌入式GUI FTK设计与实现>,没写几篇就停止更新了.当时自己研究过MicroWindows, X Window, DirectFB, GTK+和Android的GUI,又写过 ...

  7. Android 开发之 ---- 底层驱动开发(一)

    驱动概述 说到 android 驱动是离不开 Linux 驱动的.Android 内核采用的是 Linux2.6 内核 (最近Linux 3.3 已经包含了一些 Android 代码).但 Andro ...

  8. 浅析Android的窗口

    一.窗口的概念 在开发过程中,我们经常会遇到,各种跟窗口相关的类,或者方法.但是,在 Android 的框架设计中,到底什么是窗口?窗口跟 Android Framework 中的 Window 类又 ...

  9. (转) Android的Window类

    Android的Window类 2011-03-25 10:02 by Keis, 110 visits, 网摘, 收藏, 编辑 Android的Window类(一)  Android的GUI层并不复 ...

随机推荐

  1. 【十日冲刺计划】第一日 星遇Sprint1计划会议成果

    ——小组成员 赵剑峰 张傲 周龙海 Sprint 计划会议1:定出 Sprint 目标和既定产品Backlog. 会议进程(1小时) • 首先对sprint目标进行总体介绍,概括星遇的backlog, ...

  2. xhell ctrl+s 假死

    如题. ctrl+q 即可接触 假死状态. 记录下

  3. tomcat用户配置

    1.tomcat-user.xml 添加以下内容<role rolename="manager-gui"/> <role rolename="manag ...

  4. DP模板

    怕不是最后一篇(雾),过滤最基础的背包DP.状压DP.递推等 树上换根DP:https://www.luogu.org/problemnew/show/P4284 #include<bits/s ...

  5. Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之字符串类型(string)

    关于Python的字符串处理也如其他语言一样属于重点中的重点,主要是牵扯到的函数和内容较为多和乱一些.关于什么是字符串,Python中的定义是:以单引号或者双引号括起来的任意文本. 1.   字符串的 ...

  6. 【MySQL参数】-innodb_additional_mem_pool_size

    原博客:https://yq.aliyun.com/articles/32384

  7. Linux 下载安装

    安装教程:https://www.runoob.com/linux/linux-install.html Linux图形界面与命令行界面切换https://blog.csdn.net/ab522628 ...

  8. Anaconda Installation on Mac: conda command not found 环境变量配置

    Mac系统安装完Anaconda 3.7后在terminal输入conda --version,返回command not found 原因可能是没有配置环境变量 在terminal输入vi ~/.b ...

  9. spring 事物面试题

    1.spring 事物管理器中事物传播机制 2.spring中事物的隔离级别 读未提交-事物未提交,另一个事物可以读取到,脏读 读已提交-事物已提交,先前读取的数据与后来读取的数据不同,不可重复读 可 ...

  10. Python笔记_第一篇_面向过程_第一部分_9.Ubuntu基础操作

    第一部分   Ubuntu简介 Ubuntu(乌班图)是一个机遇Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“Ubuntu”一词,意思是“人性”.“我的存在 ...