Android(3)—UI相关

本文详细介绍Android UI设计,包括常见控件如TextView、Button等的使用方法及属性介绍,以及线性布局、相对布局等基本布局的特性。同时,还探讨了自定义控件的基本原理。

这部分就看看关于Android的UI设计相关了。

(补充,除了使用匿名类来对按钮进行事件触发响应外,还可以使用接口:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button btn1 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.button1:
                break;
            default:
                break;
        }
    }
}


常见控件:

TextView(文本)、Button(按钮)、EditText(编辑框)、ImageView(显示图片)、ProgressBar(进度条)、AlertDialog(弹出对话框)

ProgressDialog(弹出一个进度条对话框。。使用方式与AlertDialog比较相似)

对话框的使用方式:

ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setTitle("This is a progressDialog");
                progressDialog.setMessage("Loading...");
                progressDialog.setCancelable(true);
                progressDialog.show();
效果如下:



AlertDialog.Builder dialog = new AlertDialog.Builder(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 dialog , int which){}
                });
                dialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialog.show();


可以在点击按钮或者什么其他的情况下使用:

效果如下

但是这段代码的setButton这部分代码有点懵,对于DialogInterface完全不了解


关于控件的属性:

android:layout_height | android:layout_width 

可选值有三种match_parentfill_parentwrap_content 其中match_parent fill_parent的意义相同现在官方更加推荐使用 match_parent

match_parent表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小。

wrap_content表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小。 

android:gravity|android:layout_gravity :可以指定控件的对齐方式。

可选值:top、bottom、left 、right、center。可以使用|来同时指定多个值。

需要注意的是,gravity的对齐是针对于控件所在的直接容器位置大小来算的(所谓直接容器的位置大小:也即指定的android:layout_width 和 height)

而 layout_gravity是相对于整个页面的布局来算的。
android:text:控件的文字内容

内容即文本

android:textSize | android:textColor

文本大小和文本颜色。注意文本大小使用sp做单位: textSize="15sp"   textColor="#00ff00"

android:textAllCaps

可选值:false 和  true   控制 大小写  false  关闭默认的大写    true  开启

android:hint

提示性的文字,可能该属性只存在于输入框控件。没有尝试过,但是应该没错才对

android:maxLines

设置最大行数(对于输入框而言),即最多出现几行可显示

android:src

设置图片的来源位置(或者其他什么东西,反正我目前就知道有图片:ImageView)

style:

这个不是很懂,不过可以用来设置活动的显示方式(android:theme="@style/Theme.AppCompat.Dialog"),

进度条的类型(style="?android:attr/progressBarStyleHorizontal"),还有其他的暂时不知道


基本布局:


线性布局:

LinearLayout

通过 orientation 设置为 vertical 或者 horizontal 来控制是 垂直方向上依次排列还是水平方向是一次排列

需要注意的是,设置为 vertical 的时候,一定不能设置控件的 layout_height为match_parent

设置为 horizontal 的时候,一定不能设置控件的layout_width为match_parent。否则一个控件就直接填充满整个 活动空间了

一个重要的属性;

android:layout_weight

当使用了这个属性的时候,宽度就不再有layout_width来控制。这里的这个weight可以理解为比重。

eg:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
   <EditText
       android:id="@+id/editText"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="3"
       />
    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        />
</LinearLayout>
水平情况下,分别设置两个控件的weight为3和2则EditText占3份,Button占2份。

相对布局:

RelativeLayout

属性:

android:layout_alignParentLeft:对齐到父级的左边android:layout_alignLeft:对齐到另一个控件的左边

android:layout_alignParentTop:对齐到父级的顶部android:layout_alignTop:对齐到另一个控件的顶部

android:layout_alignParentRight对齐到父级的右边 android:layout_alignRight 对齐到另一个控件的右边

android:layout_alignParentBottom:对齐到父级的底部android:layout_alignBottom:对齐到另一个控件的底部

android:layout_centerInParent处于中间位置

帧布局:

FrameLayout:

感觉帧布局有点懵。不是很明白

这种布局没有任何的定位方式,所有的控件都会摆放在布局的左上角。


百分比布局:

只有LinearLayout支持按比例改变控件的大小(通过layout_weight实现)

百分比布局允许直接指定控件在布局中所占的百分比。

注意百分比只为FrameLayout和RelativeLayout进行了功能扩展(因为LinearLayout已经可以控制比例了。)

现在build.gradle文件中添加:compile 'com.android.support.percent:25.3.1'  (因为我的是 v7:25.3.1)

使用时,需要引入包到xml文件:

xmlns:app="http://schemas.android.com/apk/res-auto"

PercentFrameLayout

app:layout_widthPercent="50%"

app:layout_heightPercent="50%"

PercentRelativeLayout



自定义控件:

我们所用的所有控件都是直接或间接继承自 View 的,所用的所有布局都是直接或间接继承自ViewGroup 的。ViewAndroid 中一种最基本的 UI 组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件,因此,我们使用的各种控件其实就是在View 的基础之上又添加了各自特有的功能。而ViewGroup 则是一种特殊的View,它可以包含很多的子 View 和子 ViewGroup,是一个用于放置控件和布局的容器。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值