標籤:
1.TextView
以下只是一部分屬性,還有很多屬性需要在用到時候再說
<TextView
android:textSize="24sp"//文字大小
android:textColor="#00ff00"//文字顏色
android:gravity="center"//排列方向
android:id="@+id/txtMainOne"
android:text="這是一個正規的活動介面"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
2.Button
<Button
android:id="@+id/btnMainOne"
android:text="按鈕"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
在java代碼中給按鈕添加事件可以統一處理,方法是讓類去實現介面View.OnClickListener
public class MainActivity extends Activity implements View.OnClickListener {
//在onCreate方法中綁定按鈕的監聽事件為類本身
Button btnMainOne=(Button)findViewById(R.id.btnMainOne);
btnMainOne.setOnClickListener(this);
//其他代碼。。。。。。以下是對點擊按鈕的集中處理方式
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnMainOne:
//編寫邏輯
break;
default:
break;;
}
}
3.EditText
<EditText
android:id="@+id/txtMainTwo"
android:hint="起到提示資訊的作用"
android:maxLines="2"//限制文本輸入框只有2行,不會因為內容過多而出現控制項無限拉長
android:layout_width="match_parent"
android:layout_height="wrap_content" />
//擷取EditText 中的值
EditText txtMainTwo=(EditText)findViewById(R.id.txtMainTwo);
String value=txtMainTwo.getText().toString();
Toast.makeText(MainActivity.this,value,Toast.LENGTH_SHORT).show();
4.ImageView
<ImageView
android:id="@+id/imgMainOne"
android:src="@drawable/ic_launcher"//圖片地址
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
ImageView imageView=(ImageView)findViewById(R.id.imgMainOne);
imageView.setImageResource(R.drawable.one);//修改圖片
5.ProgressBar顯示進度
<ProgressBar
android:visibility="visible"//所有空間都有這個熟悉,visible,invisible和gone,分別表示顯示空間,隱藏控制項但是佔用位置,直接刪除控制項
android:id="@+id/pgbMainOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"//設定為橫向表現
android:background="#ff5b7fff"//背景顏色
android:max="100"/>//最大值
代碼控制顯示
ProgressBar progressBar=(ProgressBar)findViewById(R.id.pgbMainOne);
if(progressBar.getVisibility()==View.GONE)
{
progressBar.setVisibility(View.VISIBLE);
}
else
{
progressBar.setVisibility(View.GONE);
}
//設定進度條
int progress=progressBar.getProgress();
progress+=10;
progressBar.setProgress(progress);
6.AlertDialog
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("提示資訊");
dialog.setMessage("很重要的資訊");
dialog.setCancelable(false);
dialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
7.ProgressDialog
結合多線程展示
final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setTitle("提示");//設定標題
progressDialog.setCanceledOnTouchOutside(false);//是否在空白處點擊返回主介面
progressDialog.setMax(100);//設定為橫向進度條最大值
progressDialog.setMessage("提示內容資訊");//提示內容
progressDialog.setCancelable(false);//是否選擇取消鍵返回
progressDialog.setProgress(40);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設定為橫向進度
progressDialog.setIcon(R.drawable.one);//設定表徵圖
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"確定",new DialogInterface.OnClickListener() {//添加按鈕事件
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"按下按鈕",Toast.LENGTH_LONG).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
progressDialog.show();//顯示
new Thread(new Runnable() {//建立多線程
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新進度條的進度,可以在子線程中更新進度條進度
progressDialog.incrementProgressBy(1);//設定進度條增加值
// dialog.incrementSecondaryProgressBy(10)//二級進度條更新方式
i++;
} catch (Exception e) {
// TODO: handle exception
}
}
progressDialog.dismiss();//在進度條走完時刪除progressDialog
}
}).start();//啟動多線程
8.布局:常用的4種布局LinearLayout,RelativeLayout,FrameLayout
LinearLayout:線性布局方式,android:orientation="vertical"表示豎向排列,android:orientation="horizontal"表示橫向排列
android:layout_gravity="right"//設定控制項的位置,當是橫向的時候只能設定豎向的位置,當為豎向的時候只能設定橫向的位置。
當設定android:orientation="horizontal"時可以設定控制項的寬度是0:android:layout_width="0dp",然後設定寬度上的權重android:layout_weight="1"表示佔用的比例
RelativeLayout:相對位置布局,可以設定對應的屬性設定相對於父元素的座標或者某個控制項的座標
相對於父元素:
layout_alignParentLeft
layout_alignParentTop
layout_alignParentRight
layout_centerInParent
layout_alignParentBottom
相對於某個控制項:
android:layout_above="@+id/btnBThree"//相對於某個控制項的上方
android:layout_below="@+id/btnBThree"//相對於某個空間的下方
android:layout_toLeftOf="@+id/btnBThree"
android:layout_toRightOf="@+id/btnBThree"
android:layout_toStartOf="@+id/btnBThree"
android:layout_toEndOf="@+id/btnBThree"
android:layout_alignBottom="@+id/btnBThree"
android:layout_alignTop="@+id/btnBThree"
android:layout_alignRight="@+id/btnBThree"
android:layout_alignLeft="@+id/btnBThree"
android:layout_alignEnd="@+id/btnBThree"
android:layout_alignStart="@+id/btnBThree"
FrameLayout:所有的控制項都會在左上方定位,會出現重疊現象,不常用,但是在片段的章節將會出現
TableLayout:允許以表格的方式排列控制項,不常用。
9.自訂控制項
先設計一個layout檔案,然後再別的頁面引入已經設計好的介面:<include layout="@layout/title"/>
如果是自訂控制項:先定義一個類繼承至LinearLayout,在建構函式中編寫該類的事件
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context,AttributeSet attrs)
{
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);
Button btnTitleOne=(Button)findViewById(R.id.title_back);
btnTitleOne.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((Activity)getContext()).finish();//找到當前所在活動
}
});
Button btnTitleTwo=(Button)findViewById(R.id.title_edit);
btnTitleTwo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"你選擇了編輯按鈕",Toast.LENGTH_SHORT).show();
}
});
}
}
開始使用該使用者控制項
<com.example.zhb.test2.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.example.zhb.test2.TitleLayout>
android入門到熟練(三)----UI介面