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. 远程桌面,出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正

    问题点: 升级至win10 最新版本10.0.17134,安装最新补丁后无法远程win server 2016服务器,报错信息如下: 出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密 ...

  2. html中的标签总结

    HTML <ul> 元素(或称 HTML 无序列表元素)表示一个内可含多个元素的无序列表或项目符号列表 <ol>元素中的顺序是有意义的 <ul> 元素用来将没有数字 ...

  3. 洛谷 P1060开心的金明

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱就行”.今 ...

  4. 3分钟搞定高逼格PPT封底——简约型

    封底想要高逼格又简约? 发现了这五类,看完不会制作算我输.   一.纯文字 白色背景下,一段结束语,或提问或感谢.       重叠文字,看上去非常有创意. 没有操作难度,END放大字号,颜色设置为浅 ...

  5. Eclipse Springboot项目Dokcer

    配置好Dockerfile FROM openjdk:8-jdk-alpine ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOIN ...

  6. 关于js返回上一页的实现方法

    以前在提交表单的时候,如果提交出错返回的时候信息内容全没了,我不知道要怎么保存,就开始了那种最愚蠢的做法,将填写的数据设置到session中,让后取出来用,不过没有试成功,总是有错,无意之中在我那本j ...

  7. lvm镜像卷

    镜像能够分配物理分区的多个副本,从而提高数据的可用性.当某个磁盘发生故障并且其物理分区变为不可用时,你仍然可以访问可用磁盘上的镜像数据.LVM在逻辑卷内执行镜像. 系统版本 # cat /etc/ce ...

  8. python编程:从入门到实践----基础知识>第4章练习

    4-1 比萨 :想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来. a.修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称.对于每种 ...

  9. python爬取淘宝数据之遇到的问题

    1.chormedriver.exe驱动下载地址 https://npm.taobao.org/mirrors/chromedriver 2.跳转网页页面不稳定问题 添加智能等待时间 driver.i ...

  10. Promoter complex|转录组水平RNA的复杂度|

    生命组学 Promoter complex Tata box识别位点 Enhancer加入之后增强转录 不确定性与确定性之间的关系,原因中存在这不确定性,但是结果表达又是确定的.因为promoter的 ...