Android Tips

初學者筆記:

1,網絡連接探測: 

 boolean testNetwork(){
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
        NetworkInfo networkinfo = manager.getActiveNetworkInfo(); 
        if (networkinfo == null  ||  !networkinfo.isAvailable()) { 
             Toast.makeText(this,  "No avaliable network",  Toast.LENGTH_LONG).show();
             return false;
        }
        return true;

 }


2,Anroid Apk自動升級更新

http://blog.csdn.net/xjanker2/article/details/6303937

3,各種控件使用注意:

1),Toast用於提示用戶,使用圖片的例子

Toast toast = new Toast(this);
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.icon);
toast.setView(view);
toast.show(); 

使用文字對話框的例子:

Toast toast = Toast.makeText(this, "lalalal", Toast.LENGTH_LONG);
View textView = toast.getView();
LinearLayout lay = new LinearLayout(this);
lay.setOrientation(LinearLayout.HORIZONTAL);
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.icon);
lay.addView(view);
lay.addView(textView);
toast.setView(lay);
toast.show();

2)TabHost中設置標籤Tab的高度和寬度

final TabWidget tabWidget = tabHost.getTabWidget();
for (int i =0; i < tabWidget.getChildCount(); i++) { 
            tabWidget.getChildAt(i).getLayoutParams().height = YOUR_HEIGHT; 
            tabWidget.getChildAt(i).getLayoutParams().width = YOUR_WEIGHT;
}

注意,如果要是設置的高度和寬度有效,在界面定義文件中TabWidget的佈局參數的寬度和高度應設置爲 "wrap_content",如

<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" />

TabHost去掉標籤下方的白線:

tabHost.setPadding(tabHost.getPaddingLeft(),
                tabHost.getPaddingTop(), tabHost.getPaddingRight(),
                tabHost.getPaddingBottom()-5);


也可以修改XML:使用tabStripEnabled

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"  >          
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_alignParentBottom="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
	   android:tabStripEnabled="false"...

 

3),Android字體加粗

在xml文件中使用android:textStyle=“bold”;

但是對中文無效,設置中文爲粗體的方法是:

TextView tv = (TextView)findViewById(R.id.TextView01);
TextPaint tp = tv.getPaint();
tp.setFakeBoldText(true);



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章