Tricks of Android's GUI

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)

posted @ 2015-10-19 23:00  mlhy  阅读(121)  评论(0编辑  收藏  举报