Android简单控件

1.文本显示

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

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

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

        @Overrideprotected 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方法,即可指定文本的大小

     @Overrideprotected 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:它专门用来设置字体的大小,在系统设置中可以调整字体的大小
     <TextViewandroid:id="@+id/tv_px"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello"android:textSize="30px"/><TextViewandroid:id="@+id/tv_dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello"android:textSize="30dp"/><TextViewandroid:id="@+id/tv_sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello"android:textSize="30sp"/>
    

设置文本的颜色:

    @Overrideprotected 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);}
    <TextViewandroid: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为单位的具体尺寸
   <TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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方法,填入修改后的布局参数即可
  <TextViewandroid: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 {@Overrideprotected 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"><!-- 中间的布局背景为黄色  --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFF99"android:layout_margin="20dp"android:padding="60dp"><!-- 最内层的视图背景为红色 --><Viewandroid: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"><!-- 第一个子布局背景为红色 它在上级视图中朝下对齐,它的下级视图在则靠左对齐--><LinearLayoutandroid: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 且背景为青色--><Viewandroid:layout_width="100dp"android:layout_height="100dp"android:background="#00ffff"/></LinearLayout><!-- 第二个子布局背景为红色 它在上级视图中朝上对齐,它的下级视图在则靠左对齐--><LinearLayoutandroid: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 且背景为青色--><Viewandroid: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">
<!--<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="横排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="横排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="竖排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="竖排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout>--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="横排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="横排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"android:text="竖排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid: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"><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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"><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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><TextViewandroid: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"><HorizontalScrollViewandroid:layout_width="wrap_content"android:layout_height="200dp"><!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色--><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="horizontal"><Viewandroid:layout_width="300dp"android:layout_height="match_parent"android:background="#aaffff"></View><Viewandroid:layout_width="300dp"android:layout_height="match_parent"android:background="#ffff00"></View></LinearLayout></HorizontalScrollView><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="400dp"android:background="#aaffff"></View><Viewandroid: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"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="下面按钮英文默认大写"android:gravity="center"android:textColor="@color/black"android:textSize="17sp"></TextView><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Hello World"android:textColor="@color/black"android:textSize="17sp"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="下面按钮英文保持原状"android:gravity="center"android:textColor="@color/black"android:textSize="17sp"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Hello World"android:textAllCaps="false"android:textColor="@color/black"android:textSize="17sp"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="直接指定点击方法"android:textAllCaps="false"android:textColor="@color/black"android:textSize="17sp"android:onClick="doClick"/><TextViewandroid: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;@Overrideprotected 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"><Buttonandroid:id="@+id/btn_click_single"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="指定单独的点击监听器"android:textColor="#000000"android:textSize="15sp"/><TextViewandroid: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;@Overrideprotected 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;}@Overridepublic 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"><Buttonandroid:id="@+id/btn_long_click"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="指定长按的点击监听器"android:textColor="#000000"android:textSize="15sp"/><TextViewandroid: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 {@Overrideprotected 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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid: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><Buttonandroid: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><Buttonandroid: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><TextViewandroid: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;@Overrideprotected 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);}@Overridepublic 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"><ImageViewandroid: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 {@Overrideprotected 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:指定图片与文字的间距
        <Buttonandroid: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.ImageButtonImageButton是显示图片的按钮,但它继承自ImageView,而非继承ButtonImageButton和Button之间的区别有:- Button既可显示文本也可显示图片,ImageButton只能显示图片不能显示文本
- ImageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸图像
- Button只能靠背景显示一张图片,而ImageButton可分别在前景和背景显示图片,从而实现两张图片添加的效果#### 3.同时展示文本与图像同时展示文本与图像的可能途径:- 利用LinearLayout对ImageView和TextView组合布局- 通过按钮控件Button的drawable***属性设置文本周围图标- drawableTop:指定文字上方的图片- drawableBottom:指定文字下方的图片- drawableLeft:指定文字左边的图片- drawableRight:指定文字右边的图片- drawablePadding:指定图片与文字的间距```xml<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="图标在左"android:drawableLeft="@drawable/apple"android:drawablePadding="5dp"/>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/608414.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

人逢三六九,运势低迷路难走

很多人都希望自己各方面的运势都能够朝着好的方向旺盛发展&#xff0c;我们的运势将会如何发展跟我们的出生时间是离不开关系的。我们的祖先为了后人着想&#xff0c;不犯他们犯过的错误&#xff0c;少走一些弯路&#xff0c;总结了多年来的生活经验&#xff0c;创造出了一句句…

前端表格使用vxe-table进行渲染,使用el-select进行选择合适的条件查询,且给el-select默认赋值及使用i18n进行翻译

前端表格使用vxe-table进行渲染&#xff0c;使用el-select进行选择合适的条件查询&#xff0c;且给el-select默认赋值及使用i18n进行翻译 一、项目需求及项目实现 一、项目需求及项目实现 表格页面使用vxe-table进行渲染&#xff0c;查询区使用el-select进行选择&#xff0c;且…

JS、Python实现AES加密,Python实现RSA加密,读txt每行数据存入列表

1. JS实现AES加密 var CryptoJS require(./package); // package文件是cryptoJS var plaintext {"did":"a","dname":"Chrome"};var key CryptoJS.lib.WordArray.create([929392180,1650538033,1681023538,1647784550 ]); var i…

拼多多API:从数据中挖掘商业价值的力量

随着大数据时代的来临&#xff0c;数据已经成为企业决策和创新的基石。拼多多API作为电商领域的重要接口&#xff0c;为企业提供了从数据中挖掘商业价值的机会。通过拼多多API&#xff0c;企业可以获取丰富的用户数据、商品数据和交易数据&#xff0c;从而深入了解市场需求、优…

metric,log

Metric&#xff08;指标&#xff09;和 Log&#xff08;日志&#xff09;是两种不同的监控数据类型&#xff0c;它们在监控和故障排查中有不同的用途和特点。 ### Metric&#xff08;指标&#xff09;: 1. 定义&#xff1a; Metric 是定量的度量&#xff0c;通常是数值或计数…

C++11新特性(也称c++2.0)

目录 1.输出C版本&#xff1a;cout << __cplusplus << endl; 2.Uniform Initialization(一致性初始化) 3.initializer_list&#xff08;形参&#xff09; 4.explicit 5.for循环的新用法 6.default和delete 7.Alias Template&#xff08;模板化名&#xff09…

LLaMA Efficient Tuning

文章目录 LLaMA Efficient Tuning安装 数据准备浏览器一体化界面单 GPU 训练 train_bash1、预训练 pt2、指令监督微调 sft3、奖励模型训练 rm4、PPO 训练 ppo5、DPO 训练 dpo 多 GPU 分布式训练1、使用 Huggingface Accelerate2、使用 DeepSpeed 导出微调后的模型 export_model…

Volcano Scheduler调度器源码解析

Volcano Scheduler调度器源码解析 本文从源码的角度分析Volcano Scheduler相关功能的实现。 本篇Volcano版本为v1.8.0。 Volcano项目地址: https://github.com/volcano-sh/volcano controller命令main入口: cmd/scheduler/main.go controller相关代码目录: pkg/scheduler 关联…

Vue3.x+Echarts (可视化界面)

Vue3.0Echarts &#xff08;可视化界面&#xff09; 1. 简介1.1 技术选型1.2 ECharts支持的数据格式1.3 ECharts使用步骤 2. ECharts图形2.1 通用配置2.2 柱状图2.3 折线图2.4 散点图2.5 直角坐标系常用配置2.6 饼图2.7 地图2.8 雷达图2.9 仪表盘2.10 小结 3. Vue3.2ECharts5数…

RecombiMAb anti-mouse VEGFR-2

DC101-CP132单克隆抗体是原始DC101单克隆的重组嵌合型抗体。可变结构域序列与原始DC101相同&#xff0c;但是恒定区序列已经从大鼠IgG1变为小鼠IgG2a。DC101-CP132单克隆抗体像原始大鼠IgG1抗体一样&#xff0c;不包含Fc突变。 DC101-CP132单克隆抗体能与小鼠VEGFR-2(血管内皮生…

docker容器内,将django项目数据库改为postgresql

容器为ubuntu20.04版本&#xff0c;新建了一套django项目&#xff0c;使用的默认sqllit3&#xff0c;换为postgresql&#xff0c;容器里安装postgresql方法 步骤1: 安装PostgreSQL数据库 # 打开一个bash会话在你的容器中 docker exec -it <container_id_or_name> bash#…

ZGC垃圾收集器介绍

ZGC&#xff08;The Z Garbage Collector&#xff09;是JDK 11中推出的一款低延迟垃圾回收器&#xff0c;它的设计目标包括&#xff1a; 停顿时间不超过10ms&#xff1b;停顿时间不会随着堆的大小&#xff0c;或者活跃对象的大小而增加&#xff1b;支持8MB~4TB级别的堆&#x…

【开题报告】基于JavaWeb的母婴用品在线商城的设计与实现

1.选题背景 随着社会经济的发展和人们生活水平的提高&#xff0c;母婴用品市场逐渐壮大。同时&#xff0c;互联网的普及和电子商务的兴起使得线上购物成为了人们方便快捷的购物方式之一。传统的实体母婴用品店面受到了线上商城的冲击&#xff0c;因此建立一个方便、快捷的在线…

微信小程序 获取地址信息(uniapp)

参考API地址&#xff1a;微信小程序JavaScript SDK | 腾讯位置服务 <script> // 引入SDK核心类&#xff0c;js文件根据自己业务&#xff0c;位置可自行放置var QQMapWX require(../../js/uploadImg/qqmap-wx-jssdk.js);export default {data(){return{qqmapsdk:}},onL…

【HarmonyOS4.0】第四篇-ArkUI基础实战

一、ArkUI框架简介 ArkUI开发框架是方舟开发框架的简称&#xff0c;它是一套构建 HarmonyOS / OpenHarmony 应用界面的声明式UI开发框架&#xff0c;它使用极简的UI信息语法、丰富的UI组件以及实时界面语言工具&#xff0c;帮助开发者提升应用界面开发效率 30%&#xff0c;开发…

Swift单元测试Quick+Nimble

文章目录 使用QuickNimble1、苹果官方测试框架XCTest的优缺点2、选择QuickNimble的原因&#xff1a;3、QuickNimble使用介绍集成&#xff1a;Quick关键字说明&#xff1a;Nimble中的匹配函数等值判断&#xff1a;使用equal函数是否是同一个对象&#xff1a;使用beIdenticalTo函…

Android14之刷机模式总结(一百七十八)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

电脑文件mfc100u.dll丢失的解决方法分析,怎么修复mfc100u.dll靠谱

mfc100u.dll丢失了要怎么办&#xff1f;其实很多人都遇到过这样的电脑故障吧&#xff0c;说这个mfc100u.dll文件已经不见了&#xff0c;然后一些程序打不开了&#xff0c;那么这种情况我们要怎么解决呢&#xff1f;今天我们就来给大家详细的说说mfc100u.dll丢失的解决方法。 一…

#Css篇:flex布局的总结

注意&#xff0c;设为flex布局以后&#xff0c;子元素的float、clear、vertical-align属性将失效。 概念 采用flex布局的元素&#xff0c;简称“容器”。内部的子元素&#xff0c;简称“项目”。 容器存在两根轴&#xff0c;水平主轴main axis,开始叫 main start;结束叫 main…