UI开发的点点滴滴

一、常见控件的使用方法。

1、TextView

 <TextView
     android:id="@+id/text_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="This is TextView"
     android:gravity="center"
     android:textSize="32sp"
     android:textColor="#00ff00" />

2、Button

 <Button 
     android:id="@+id/button"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="This is Button"/>

3、EditText:可以结合Button输出内容:

edit.getText().toString();
  <!-- hint 再输入框里显示一些提示性的文字 -->
 <EditText 
     android:id="@+id/editText"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="Type something here" 
     android:maxLines="2"
     />

4、ImageView

 <ImageView 
     android:id="@+id/imageView"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher"/>

5、ProgressBar

 <ProgressBar 
     android:id="@+id/progressBar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     style="?android:attr/progressBarStyleHorizontal"
     android:max="100"/>

设置进度条的样式,圆形进度条和水平进度条。

所有Android控件都具有这个属性,可以通过android:visibility设置控件的可见属性。visible、invisible、gone.

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都是用于提示一些非常重要的内容或者警告消息。

    AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
            dialog.setTitle("This is a dialog");//设置标题
            dialog.setMessage("Something important");//设置提示内容
            dialog.setCancelable(false);//是否可以通过Back键取消
            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {//设置确定按钮的事件
                
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    
                }
            });
            dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//设置取消按钮事件
                
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    
                }
            });
            dialog.show();//显示按钮

 二、详解四种基本布局

1、LinearLayout

  • android:orientation 该属性指定排列方向。vertical是垂直方向,horizontal是水平方向。
  • android:layout_gravity:控件在布局中的对齐方式。
  • android:layout_weight:允许我们使用比例的方式指定控件的大小
   <EditText 
       android:id="@+id/editText"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:hint="Type something"/>
    <Button 
        android:id="@+id/button"
        android:layout_width="w"
        android:layout_height="wrap_content"
        android:layout_weight="0" 
        android:text="Send"/>

2、RelativeLayout:相对布局

3、FramLayout:没有任何定位方式

4、TableLayout:允许使用表格的方式来排列控件

android:stretchColumns:属性可以将TableLayout中的某一列进行拉伸,以达到自动适应屏幕宽度的作用。0指定第一列,依次类推。

android:layout_span:该控件占据几列,默认是1.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1" >
    <TableRow >
        <TextView 
            android:layout_height="wrap_content"
            android:text="Account:"/>
        <EditText 
            android:id="@+id/editTextAccount"
            android:layout_height="wrap_content"
            android:hint="Input your Account"/>
    </TableRow>
    
    <TableRow >
        <TextView 
            android:layout_height="wrap_content"
            android:text="Password:"/>
        <EditText 
            android:id="@+id/editTextPassword"
            android:layout_height="wrap_content"
            android:hint="Input your Password"/>
        
    </TableRow>
    <TableRow >
       <Button 
           android:id="@+id/button"
           android:layout_height="wrap_content"
           android:layout_span="2"
           android:text="Login"/>
        
    </TableRow>
</TableLayout>
原文地址:https://www.cnblogs.com/lyjs/p/5265445.html