Tips for Android

1.Window.FEATURE_NO_TITLE

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

}
you must declar requestWindowFeature(Window.FEATURE_NO_TITLE); before super.onCreate(savedInstanceState); and this extends Activity.

2.Using Layout Resources Programmatically
You inflate the layout file into a View object using the LayoutInflater class.
LayoutInflater inflater = LayoutInflater.from(context);
View exampleView = inflater.inflate(R.layout.example, container,false);
Button myExampleButton = (Button) exampleView.findViewById(R.id.button);

<include layout="@layout/basicHeader"/>
3.TextView,EditTest, Button, Spinner, AutoCompleteTextView, ProgressBars, SeekBars, AsyncTask, ImageViews.
Button extended a TextView and ImageButton extended ImageView.
ProgressBar(indeterminate, determinate);
ShowProgressTask showTask = new ShowProgressTask();
showTask.execute();

private class ShowProgressTask extends AsyncTask<Void, Integer, Integer> {
@Override protected void onPreExecute(){mProgressBar.setVisibility(View.VISIBLE);}
@Override protected Integer doInBackground(Void... params){
    for(int i=0;i<=100;i++){try{Thread.sleep(100);
                                           publishProgress(i);// make a call to onProgressUdate.
                                                          }
                                           catch(InterruptedException e){return -1;} }
    }return 100;
  }
@Override protected void onProgressUpdate(Integer...progess){
    Int progress=progess[0]; mHorizontalProgressBar.setProgress(progress);
    mSeekBar.setProgress(progress);
}
@Override protected void onPostExecute(Integer result){
   mProgressBar.setVisibility(View.INVISIBLE);
}

}
 Swapping Drawables in a Button
mIcon = getResources().getDrawable( R.drawable.ic_launcher);
mSkateboardIcon = getResources().getDrawable( R.drawable.robot_skateboarding);
swapButton=(Button) findViewById(R.id.button2);
swapButton.setOnclickListener(new OnClickListener(){
    public void onClick(View v){
       if(swapButton.getText().equals("Button")){
            swapButton.setText("Skkateboarder");
            swapButton.setCompoundDrawablesWithIntrinsicBounds(mSkateboardIcon,null,null,null) ;
       }else{swapButton.setText("Button");
swapButton.setCompoundDrawablesWithIntrinsicBounds(mIcon,null,null,null) ;
               }
    }
});
style="@android.style/Widget.Button.Inset"

android.background="@drawable/button_custom"
button_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/button_pressed"
           android:state_pressed="true"/>
 <item android:drawable="@drawable/button_focused"
           android:state_focused="true"/>
 <item android:drawable="@drawable/button_default"/>
</selector>
 4.ActionBar, Menu
 @Override public boolean onCreateOptionMenu(Menu menu){
   getMenuInflater().inflate(R.menu.activity_main,menu); return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
          android:orderInCategory="100"
          android:showAsAction="never"  //always,ifRoom,withText,collapseActionView
          android:title="@string/menu_settings"
          android:icon="@+drawable/menu_share"/>
</menu>
@Override public boolean onOptionsItemSelected(MenuItem item){
  Toast toast=Toast.makeText(getApplicationContext(),"Option 1", Toast.LENGTH_SHORT);
     switch(item.getItemId()){
       case R.id.menu_item_1: toast.setText("Option 1"); toast.show();
         return true;
       case R.id.menu_item_2:
         return true;
       case R.id.menu_item_3:
         return true;
       default: return super.onOptionsItemSelected(item);

    }
}
5.Drop-Down Navigation
navigation_item.xml in /res/layout folder:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"  style="?android:attr/dropDownItemStyle"
android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" android:singleLine="true" android:textColor="#eeeeee"
android:textAppearance="?android:attr/textAppearanceLargeInverse" />
String[] values = {"one","two","three","one hundred","one thousand"};
 mSpinnerAdapter = new ArrayAdapter<String>(this, R.layout.navigation_item,values);

private class NavigationListener implements ActionBar.OnNavigationListener{
 @Override public boolean onNavigationItemSelected(int position,long itemId){
  String selected = values[position];
  Toast toast=Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
   toast.show();
   return false;
 }
}
6.Tab Navigation
protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);
  ActionBar actionBar=getActionBar();
  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  mTab1=actionBar.newTab().setText("Tab 1").setTabListener(new ExampleTabListener());
 mTab2=actionBar.newTab().setText("Tab 2").setTabListener(new ExampleTabListener());
actionBar.addTab(mTab1); actionBar.addTab(mTab2);
}

private class ExampleTabListener implements ActionBar.TabListener{
  public ExampleTabListener(){}
 @Override public void onTabReselected(Tab tab, FragmentTransaction ft){}
@Overrid public void onTabSelected(Tab tab, FragmentTransaction ft){
    Toast toast=Toast.makeText(getApplicationContext(), "tab", Toast.LENGTH_SHORT);
    if(tab.equals(mTab1)){toast.setText("Tab 1");toast.show();}
       else{toast.setText("Tab 2");toast.show();}
  }
@Override public void onTabUnselected(Tab tab, FragmentTransaction ft){}
}

7.Fragment
 We can use two techniques to display this fragment in an activity: either embed the fragment in an XML layout file or add it dynamically
fragment_a.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" >
 <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="Layout for fragment A"
   android:textAppearance="?android:attr/textAppearanceLarge">
 </TextView>
</RelativeLayout>
FragmentA.java
import android.app.Fragment; android.os.Bundle;android.view.LayoutInflater;
import android.view.View;android.view.ViewGroup;
public class FragmentA extends Fragment{
 @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,
    Bundle savedInstanceState){
    View v=inflater.inflate(R.layout.fragment_a,container,false);   
}}
activity_main.xml 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"  tools:context=".MainActivity"
    android:layout_width="match_parent" android:layout_height="match_parent" >
 <fragment android:id="@+id/fragment_a"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"
    android:name="com.bffmedia.hour8app.intro.FragmentA" />
</RelativeLayout>

Displaying Fragments Dynamically
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"  tools:context=".MainActivity"
    android:layout_width="match_parent" android:layout_height="match_parent" >
 <LinearLayout android:id="@+id/layout_container"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"
   >
</LinearLayout>
</RelativeLayout>
public void showFragmentA(){
 FragmentA fragmentA=new FragmentA();
 FragmentTransaction ft=getFragmentManager().beginTransaction();
 ft.replace(R.id.layout_container, fragmentA);
 ft.addToBackStack("example");
 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
 ft.commit();
}
Button button=(Button) v.findViewById(R.id.button1);
button.setOnClickListener(new onClickListener(){
    public void onClick(View v){MainActivity currentActivity=(MainActivity) getActivity();
           currentActivity.showFragmentA();
    }
});

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE