Android简单控件

1.文本显示

设置文本内容的两种方式:

  • 在XML文件中通过属性 android:text 设置文本

    <resources>
        <string name="app_name">chapter03</string>
        <string name="hello">你好,世界</string>
    </resources>
    
    
        <TextView
            android:id="@+id/tv_hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"/>
    
    
  • 在Java代码中调用文本视图对象的 setText 方法设置文本

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_text_view);
            TextView tv_hello = findViewById(R.id.tv_hello);
            //tv_hello.setText("你好,世界");
            tv_hello.setText(R.string.hello);
    
        }
    

设置文本的大小:

  • 在Java代码中调用setTextSize方法,即可指定文本的大小

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_text_size);
            TextView tv_px = findViewById(R.id.tv_px);
            tv_px.setText(30);
    
        }
    
  • 在XML文件中则通过属性android:textSize指定文本大小,此时需要指定字号的单位

    • px:它是手机屏幕的最小显示单位,与设备的显示屏有关
    • dp:它是与设备无关的显示单位,只与屏幕的尺寸有关
    • sp:它专门用来设置字体的大小,在系统设置中可以调整字体的大小
     <TextView
            android:id="@+id/tv_px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:textSize="30px"/>
    
        <TextView
            android:id="@+id/tv_dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:textSize="30dp"/>
    
        <TextView
            android:id="@+id/tv_sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:textSize="30sp"/>
    

设置文本的颜色:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_color);
        //从布局文件中获取名叫tv_code_system的文本视图
        TextView tv_code_system = findViewById(R.id.tv_code_system);
        //将tv_code_system的文字颜色设置系统自带的绿色
        tv_code_system.setTextColor(Color.GREEN);

    }
    <TextView
        android:id="@+id/tv_code_system"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="代码设置系统自带的颜色"
        android:textSize="17sp"/>

2.视图基础

视图宽度通过属性android:layout_width表达,视图高度通过属性android:layout_height表达,宽高的取值主要有下列三种:

  • match_parent:表示与上级视图保持一致
  • wrap_content:表示与内容自适应
  • 以dp为单位的具体尺寸
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="视图宽度采用wrap_content定义"
        android:textColor="#000000"
        android:textSize="17sp" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="视图宽度采用match_parent定义"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="视图宽度采用固定大小"
        android:textColor="#000000"
        android:textSize="17sp" />

在代码中设置视图的宽高

首先确保XML中宽高的属性值为wrap_content,接着打开该页面对应的Java代码,依序执行下面的步骤:

  • 调用控件对象的getLayoutParams方法,获取该控件的布局参数
  • 布局参数的width属性表示宽度,height属性表示高度,修改这两个属性值
  • 调用控件对象的setLayoutParams方法,填入修改后的布局参数即可
  <TextView
        android:id="@+id/tv_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="通过代码来指定视图的宽度"
        android:textColor="#000000"
        android:textSize="17sp" />
public class ViewBorderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_border);
        TextView tv_code = findViewById(R.id.tv_code);
        //获取tv_code的布局参数
        ViewGroup.LayoutParams params = tv_code.getLayoutParams();
        //修改布局参数中的宽度数值,注意默认px单位,需要把dp数值转成px数值
        params.width = Utils.dip2px(this,300);
        //设置tv_code布局参数
        tv_code.setLayoutParams(params);

    }
}

设置视图的间距的两种方式:

  • 采用layout_margin属性,它指定了当前视图与周围平级视图之间的距离。包括layout_margin、layout_marginLeft、layout_marginTop、layout_marginRight、layout_marginBottom
  • 采用padding属性,它指定了当前视图与内部下级视图之间的距离。包括padding、paddingLeft、paddingTop、paddingRight、paddingBottom
<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="300dp"
    android:orientation="vertical"
    android:background="#00AAFF">

    <!-- 中间的布局背景为黄色  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF99"
        android:layout_margin="20dp"
        android:padding="60dp">

        <!-- 最内层的视图背景为红色 -->
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#F31414"/>
        />
</LinearLayout>

设置视图的对齐的两种途径:

  • 采用layout_gravity属性,它指定了当前视图相对于上级视图的对齐方式
  • 采用gravity属性,它指定了下级视图相对于当前视图的对齐方式

layout_gravity与gravity的取值包括:left、top、right、bottom,还可以用竖线连接各取值,例如left|top表示靠左又靠上

<?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="300dp"
    android:background="#ffff99"
    android:orientation="horizontal">

    <!-- 第一个子布局背景为红色 它在上级视图中朝下对齐,它的下级视图在则靠左对齐-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:layout_margin="10dp"
        android:background="#ff0000"
        android:padding="10dp"
        android:layout_gravity="bottom"
        android:gravity="left">

        <!-- 内部视图宽度和高度都是100dp 且背景为青色-->
        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#00ffff"/>

    </LinearLayout>

    <!-- 第二个子布局背景为红色 它在上级视图中朝上对齐,它的下级视图在则靠左对齐-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:layout_margin="10dp"
        android:background="#ff0000"
        android:padding="10dp"
        android:layout_gravity="top"
        android:gravity="right">

        <!-- 内部视图宽度和高度都是100dp 且背景为青色-->
        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#00ffff" />
    </LinearLayout>
</LinearLayout>

3.常用布局

1.LinearLayout

线性布局内部的各视图的各种排列方式:

  • orientation属性值为horizontal时,内部视图是水平方向从左往右排列的
  • orientation属性值为vertical时,内部视图是垂直方向从上往下排列的

如果不指定orientation的值,则LinearLayout默认水平方向排列。

线性布局的权重:指的是线性布局的下级视图各自拥有多大比例的宽高

权重的属性名叫:layout_weight,但该属性不咋哎LinearLayout结点设置,而在线性布局的直接下级视图设置,表示该下级视图占据的宽高比例。

  • layout_width填0dp时,layout_weight表示水平方向的宽度比例
  • layout_height填0dp时,layout_weight表示垂直方向的宽高比例
<?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"
    android:orientation="vertical">
<!--
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>
    </LinearLayout>-->


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>
    </LinearLayout>
</LinearLayout>
2.RelativeLayout

相对布局的下级视图位置由其他视图决定,用于确定下级视图的位置的参照物分为两种:

  • 与该视图自身平级的视图
  • 该视图的上级视图

如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="150dp">

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerInParent="true"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>


    <TextView
        android:id="@+id/tv_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerHorizontal="true"
        android:text="我在水平中间"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerVertical="true"
        android:text="我在水平中间"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_alignParentLeft="true"
        android:text="我跟上级左边对齐"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_alignParentRight="true"
        android:text="我跟上级右边对齐"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_alignParentTop="true"
        android:text="我跟上级顶部对齐"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_alignParentBottom="true"
        android:text="我跟上级底部对齐"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_toLeftOf="@+id/tv_center"
        android:layout_alignTop="@+id/tv_center"
        android:text="我在中间左边"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>


    <TextView
        android:id="@+id/tv_right_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_toRightOf="@+id/tv_center"
        android:layout_alignTop="@+id/tv_center"
        android:text="我在中间右边"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_above_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_above="@+id/tv_center"
        android:layout_alignLeft="@+id/tv_center"
        android:text="我在中间上边"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>

    <TextView
        android:id="@+id/tv_below_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_below="@+id/tv_center"
        android:layout_alignLeft="@+id/tv_center"
        android:text="我在中间下边"
        android:textSize="11sp"
        android:textColor="#000000"></TextView>
</RelativeLayout>
3.GridLayout

网格布局支持多行多列的表格排列

网格布局默认从左到右、从上到下排列,它新增了两个属性:

  • columnCount:它指定了网格的列数,即每行能放多少个视图
  • rowCount:它指定了网格的行数,即每列能放多少个视图
<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"
    android:columnCount="2"
    android:rowCount="2">
    
    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#ffcccc"
        android:text="浅红色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"></TextView>




    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#ffaa00"
        android:gravity="center"
        android:text="橙色"
        android:textColor="#000000"
        android:textSize="17sp"></TextView>


    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#00ff00"
        android:text="绿色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"></TextView>




    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#660066"
        android:text="深紫色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"></TextView>

</GridLayout>
4.ScrollView

滚动视图有两种:

  • ScrollView:它是垂直方向的滚动视图;垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content
  • HorizontalScrollView:它是水平方向的滚动视图;水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent
<?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"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="200dp">

        <!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#aaffff">

            </View>

            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#ffff00">

            </View>
        </LinearLayout>
    </HorizontalScrollView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#aaffff">

            </View>

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#ffff00">

            </View>
        </LinearLayout>
        
    </ScrollView>
</LinearLayout>

4.按钮触控

1.按钮控件

按钮空间Button由TextView派生而来,它们之间的区别有:

  • Button拥有默认的按钮背景,而TextView默认无背景
  • Button的内部文本默认居中对齐,而TextView的内部文本默认靠左对齐
  • Button会默认将英文字母转为大写,而TextView保持原始的英文大小写

与TextView相比,Button新增了两个新属性:

  • textAllCaps:它只当是否将英文字母转为大写,为true是表示自动转为大写,为false表示不做大写转换
  • conClick:它用来接管用户的点击动作,指定了点击按钮时要触发哪个方法
<?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"
    android:orientation="vertical"
    android:padding="5dp">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面按钮英文默认大写"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"></TextView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/black"
        android:textSize="17sp"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面按钮英文保持原状"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="直接指定点击方法"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="17sp"
        android:onClick="doClick"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这里查看按钮点击结果"
        android:textColor="@color/black"
        android:textSize="17sp"/>
</LinearLayout>
public class ButtonStyleActivity extends AppCompatActivity {

    private TextView tv_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_style);
        tv_result = findViewById(R.id.tv_result);

    }

    public void doClick(View view){
        String desc = String.format("%s 您点击了按钮: %s", DataUtil.getNowTime(),((Button)view).getText());
        tv_result.setText(desc);
    }
}
2.点击事件和长按事件
  • 监听器,意思是专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。

  • 按钮控件有两种常用的监听器:

    • 点击监听器,通过setOnClickListener方法设置。按钮被按住的事件少于500毫秒时,会触发点击事件。
    • 长按监听事件,通过setOnLongClickLIstener方法设置。按钮被按住的事件超过500毫秒时,会触发长按事件。
    <?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"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btn_click_single"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="指定单独的点击监听器"
            android:textColor="#000000"
            android:textSize="15sp"/>
    
    
        <TextView
            android:id="@+id/tv_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:gravity="center"
            android:text="查看按钮点击的结果"
            android:textColor="#000000"
            android:textSize="15sp"/>
    
    </LinearLayout>
    
    
    public class ButtonClickActivity extends AppCompatActivity {
    
        private TextView tv_result;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_button_click);
            tv_result = findViewById(R.id.tv_result);
            Button btn_click_single = findViewById(R.id.btn_click_single);
            btn_click_single.setOnClickListener(new MyOnClickListener(tv_result));
        }
    
    
        static class MyOnClickListener implements View.OnClickListener{
    
            private final TextView tv_result;
    
            public MyOnClickListener(TextView tv_result){
                this.tv_result = tv_result;
            }
    
    
            @Override
            public void onClick(View view) {
                String desc = String.format("%s 您点击了按钮: %s", DataUtil.getNowTime(),((Button)view).getText());
                tv_result.setText(desc);
            }
        }
    }
    
    <?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"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btn_long_click"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="指定长按的点击监听器"
            android:textColor="#000000"
            android:textSize="15sp"/>
    
    
        <TextView
            android:id="@+id/tv_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:gravity="center"
            android:text="查看按钮点击的结果"
            android:textColor="#000000"
            android:textSize="15sp"/>
    
    </LinearLayout>
    
    public class ButtonLongActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_button_long);
            TextView tv_result = findViewById(R.id.tv_result);
            Button btn_long_click = findViewById(R.id.btn_long_click);
    
            btn_long_click.setOnLongClickListener(view -> {
                String desc = String.format("%s 您点击了按钮: %s", DataUtil.getNowTime(),((Button)view).getText());
                tv_result.setText(desc);
                return true;
            });
    
        }
    }
    
3.禁用和恢复

在实际业务中,按钮拥有两种状态,即不可用状态和可用状态,它们在外观和功能上的区别如下:

  • 不可用按钮:按钮不允许点击,即使点击也没反应,同时按钮文字为灰色
  • 可用按钮:按钮允许点击,点击按钮会触发点击事件,同时按钮文字为正常的黑色

是否允许点击由enabled属性控制,属性值为true时表示允许点击,为false时表示不允许点击

<?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"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_enable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="启用测试按钮"
            android:textColor="#000000"
            android:textSize="17sp"></Button>


        <Button
            android:id="@+id/btn_disable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="禁用测试按钮"
            android:textColor="#000000"
            android:textSize="17sp"></Button>

    </LinearLayout>


    <Button
        android:id="@+id/btn_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试按钮"
        android:textColor="#888888"
        android:textSize="17sp"
        android:enabled="false"></Button>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这里查看测试按钮的点击结果"
        android:textColor="#000000"
        android:textSize="17sp"/>


</LinearLayout>
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_test;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_enable);
        Button btn_enable = findViewById(R.id.btn_enable);
        Button btn_disable = findViewById(R.id.btn_disable);
        btn_test = findViewById(R.id.btn_test);
        tv_result = findViewById(R.id.tv_result);

        btn_enable.setOnClickListener(this);
        btn_disable.setOnClickListener(this);
        btn_test.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_enable:
                //启用当前控件
                btn_test.setEnabled(true);
                btn_test.setTextColor(Color.BLACK);
                break;
            case R.id.btn_disable:
                //禁用当前控件
                btn_test.setEnabled(false);
                btn_test.setTextColor(Color.GRAY);
                break;
            case R.id.btn_test:
                String desc = String.format("%s 您点击了按钮: %s", DataUtil.getNowTime(),((Button)view).getText());
                tv_result.setText(desc);
                break;
        }
    }
}

5.图像显示

1.ImageView

图像视图展示图片通常位于res/drawable***目录,设置图像视图的显示图片有两种方式:

  • 在XML文件中,通过属性android:src设置图片的资源,属性值格式形如@drawable/不含扩展名的图片名称
  • 在Java代码中,调用setImageResource方法设置图片的资源,方法参数格式形如R.drawable.不含扩展名的图片名称

ImageView本身默认图片是居中显示,若要改变图片的显示方式,可通过scaleType属性设置:

  • fitXY:拉伸图片使其正好填满视图
  • fitStart:保持宽高比例,拉伸图片使其位于视图上方或左侧
  • fitCenter:保持宽高比例,拉伸图片使其位于视图中间
  • fitEnd:保持宽高比例,拉伸图片使其位于视图下方或右侧
  • center:保持图片原尺寸,并使其位于视图中间
  • centerCrop:拉伸图片使其充满视图,并位于视图中间
  • centerInside:保持宽高比例,缩小图片使之位于视图中间
<?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"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_scale"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/apple"
        android:scaleType="fitStart"/>

</LinearLayout>
public class ImageScaleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_scale);
        ImageView iv_scale = findViewById(R.id.iv_scale);
        iv_scale.setImageResource(R.drawable.apple);
    }
}
2.ImageButton

ImageButton是显示图片的按钮,但它继承自ImageView,而非继承Button

ImageButton和Button之间的区别有:

  • Button既可显示文本也可显示图片,ImageButton只能显示图片不能显示文本
  • ImageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸图像
  • Button只能靠背景显示一张图片,而ImageButton可分别在前景和背景显示图片,从而实现两张图片添加的效果
3.同时展示文本与图像

同时展示文本与图像的可能途径:

  • 利用LinearLayout对ImageView和TextView组合布局

  • 通过按钮控件Button的drawable***属性设置文本周围图标

    • drawableTop:指定文字上方的图片
    • drawableBottom:指定文字下方的图片
    • drawableLeft:指定文字左边的图片
    • drawableRight:指定文字右边的图片
    • drawablePadding:指定图片与文字的间距
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="图标在左"
            android:drawableLeft="@drawable/apple"
            android:drawablePadding="5dp"/>
    

tActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_scale);
    ImageView iv_scale = findViewById(R.id.iv_scale);
    iv_scale.setImageResource(R.drawable.apple);
}

}


#### 2.ImageButton

ImageButton是显示图片的按钮,但它继承自ImageView,而非继承Button

ImageButton和Button之间的区别有:

- Button既可显示文本也可显示图片,ImageButton只能显示图片不能显示文本
- ImageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸图像
- Button只能靠背景显示一张图片,而ImageButton可分别在前景和背景显示图片,从而实现两张图片添加的效果

#### 3.同时展示文本与图像

同时展示文本与图像的可能途径:

- 利用LinearLayout对ImageView和TextView组合布局

- 通过按钮控件Button的drawable***属性设置文本周围图标

  - drawableTop:指定文字上方的图片
  - drawableBottom:指定文字下方的图片
  - drawableLeft:指定文字左边的图片
  - drawableRight:指定文字右边的图片
  - drawablePadding:指定图片与文字的间距

  ```xml
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="图标在左"
          android:drawableLeft="@drawable/apple"
          android:drawablePadding="5dp"/>
  • 26
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值