1、動態顯示和隱藏控件:
layout布局:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" > <Button android:id="@+id/button1" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:visibility="gone" android:text="立即體驗" /> </RelativeLayout>
代碼設置可見:
private Button open; open=(Button)findViewById(R.id.button1); open.setVisibility(View.VISIBLE);//設置可見 open.setVisibility(View.GONE);//不可見
2、移動圖片(指示器圖片):
XML布局:(根據布局文件,cursor是在最邊上的)
<ImageView android:id="@+id/cursor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/a" />
代碼實現imageview的移動:(設置圖片的位置,以及執行動畫)
private void InitImageView() {
imageView = (ImageView) findViewById(R.id.cursor);
bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
.getWidth();//獲取圖片a的寬度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;//獲取屏幕寬度
offset = (screenW / 3 - bmpW) / 2;
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);//平行變換
imageView.setImageMatrix(matrix);//設置imageView的初始位置
}
......
Animation animation = new TranslateAnimation(startX, toX, startY, toY);
animation.setFillAfter(true);//圖片停留在動畫結速位置
animation.setDuration(300);//動畫執行時間300us
imageView.startAnimation(animation);//imageView做動畫移動
3、改變布局的方向:
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:id="@+id/linear1" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textColor="@color/mycolor" /> </LinearLayout>
代碼改變布局:
private LinearLayout linearLayout; ... linearLayout=(LinearLayout)findViewById(R.id.linear1); linearLayout.setOrientation(0);//0水平布局;1垂直布局
4、利用布局文件 / 圖片 創建View
(1)布局文件:layout/tab_item_view.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 每個選項卡的布局內容 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <!-- ImageView和Textiew不必設置顯示值,在代碼中通過view設置 --> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:padding="3dp" > </ImageView> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:textColor="#ffffff"> </TextView> </LinearLayout>
創建View,代碼部分:
private LayoutInflater layoutInflater;
layoutInflater=LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.tab_item_view, null);
// 設置一個選項卡的圖片資源(包括選中與未選中的不同圖片)
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(R.drawable.tab_home_btn);
// 設置一個選項卡的文本
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText("主頁");
return view;
(2)圖片文件創建View
private List<View> views = new ArrayList<View>();//Views列表,add(布局)
ImageView imaV; imaV = new ImageView(this); imaV.setImageResource(R.id.dr1); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT); imaV.setLayoutParams(params); imaV.setScaleType(ScaleType.FIT_XY); views.add(imaV);
5、include加載布局:
在主布局文件中加入一個RelativeLayout,並在屏幕底部顯示:
<include android:layout_alignParentBottom="true"
android:id="@+id/inlude1"
android:layout_height="wrap_content" android:layout_width="wrap_content" layout="@layout/action_btns"/>
新建action_btns.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffd9d9d9" android:orientation="horizontal"> <ImageButton android:id="@+id/list_add" style="?android:attr/buttonBarButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_weight="1.0" android:src="@drawable/ic_menu_add" /> </RelativeLayout>
6、布局屬性LayoutParams:
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); ImageView imaV; imaV.setLayoutParams(params);
7、