Android 中的UI设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunshunli/article/details/83410900

Android中有几种常用的空间,比如,TextView,Button,EditText,ImageView,ProgressBar,AlertDialog,ProgressDialog

下面我们一一介绍:

TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#00ff00"
        android:gravity="center"
        android:text="this is a view"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

android:layout_width:指定控件的宽度

android:layout_width:指定控件的高度

他们的选项有matct_parent,fill_parent,wrap_content

matct_parent:,fill_parent含义是一样的,推荐使用matct_parent,都是表示当前的控件的大小和父布局的大小一样。

wrap_content:表示当前的控件的大小能够刚好包含里面的内容。

android:textSize="24sp"这个是设置文本的字体大小的
android:textColor="#00ff00"这个是设置文本颜色的
android:gravity="center"这个是设置文本的位置的

还有其他的属性,大家可以自己查一查,这里就不一一列举了。

Button:

<Button
        android:id="@+id/btn1"
        android:text="this is a button"
        android:textAllCaps="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
android:textAllCaps="false"这句话意思是,我们的android系统默认会把我们的按钮上的字符给去全部大写。所以我们给他一个false。

EditText

<EditText
        android:id="@+id/et1"
        android:hint="我们会有提示,还没有输入的时候"
        android:maxLines="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
android:hint="我们会有提示,还没有输入的时候"//我们希望在我们没输入东西的时候在输入框里会有提示信息。
android:maxLines="2"我们的输入框中最多输入两行,如果在多的话,就会网上滚动。

 ImageView

<ImageView
        android:id="@+id/im1"
        android:src="@drawable/ming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
android:src="@drawable/ming"//自定我们图片的资源位子,我们一般吧图片放在drawable文件夹下。

ProgressBar:

<ProgressBar
        android:id="@+id/pb1"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="500"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
style="?android:attr/progressBarStyleHorizontal"//长条形的进度,默认的是圆形
android:max="500"//设置进度条的大小

AlertDialog:可以在当前你的页面弹出一个对话框,这个对话框是置顶在所有的页面元素之上的,能够屏蔽掉其他控件的交互能力。

 AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                        dialog.setTitle("This is a dialog");
                        dialog.setMessage("Something importance");
                        dialog.setCancelable(false);
                        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        dialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        dialog.show();

首先通过AlertDialog.Builder创建一个AlertDialog对象实例,然后可以为这个对话框设置标题,内容,可否取消等属性,接下来调用setPositiveButton()方法设置取消确定按钮的事件,同理,最后调用show()方法来新式出来。

猜你喜欢

转载自blog.csdn.net/sunshunli/article/details/83410900
今日推荐