前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >速读原著-Android应用开发入门教程(作为简单容器使用的视图组)

速读原著-Android应用开发入门教程(作为简单容器使用的视图组)

作者头像
cwl_java
发布2020-02-13 15:14:55
7660
发布2020-02-13 15:14:55
举报
文章被收录于专栏:cwl_Java

8.3 作为简单容器使用的视图组

8.3.1.单选按钮组

单选按钮组(RadioButton)是一组逻辑上相关的按钮,它们之中只能有一个被选中,单选按钮通常单选按钮被设计成圆形的外观。因此需要一个类将各个单选按钮包含在一起。

参考示例程序:RadioGroup1(ApiDemo=>Views=>Radio Group) 源代码:com/example/android/apis/view/RadioGroup1.java 布局文件:radio_group_1.xml RadioGroup1 程序的运行结果如图所示:

在这里插入图片描述
在这里插入图片描述

此程序使用 RadioGroup 将几个单选按钮组织在一起,RadioGroup 的扩展关系如下:

代码语言:javascript
复制
 => android.view.View 
 => android.view.ViewGroup 
 => android.widget.LinearLayout 
 => android.widget.RadioGroup

RadioGroup 本身扩展了线性布局,它的功能比较单一,是为了保证多个 RadioButton 只有一个被选中,这种关系通常也被称为多选互斥(multiple-exclusion)。

使用 RadioGroup 组成一个单选列表,需要将 RadioButton 放置在一个 RadioGroup 中。本例的布局文件内容如下所示:

代码语言:javascript
复制
<RadioGroup android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:orientation="vertical" android:checkedButton="@+id/lunch" 
 android:id="@+id/menu"> 
 <RadioButton android:text="@string/radio_group_1_breakfast" 
 android:id="@+id/breakfast" /> 
 <RadioButton android:text="@string/radio_group_1_lunch" 
 android:id="@id/ lunch" /> 
 <RadioButton android:text="@string/radio_group_1_dinner" 
 android:id="@+id/ dinner" /> 
 <RadioButton android:text="@string/radio_group_1_all" 
 android:id="@+id/ all" /> 
 <TextView android:text="@string/radio_group_1_selection" 
 android:id="@+id/ choice" /> 
</RadioGroup>

RadioGroup 中的 XML 属性 android:checkedButton 表示这一组单选按钮 RadioButton 组中被选中的按钮,包含在一个 RadioGroup 之中的所有单选按钮只能有一个被选中。

根据扩展关系RadioGroup本身即是ViewGroup,也是LinearLayout,因此在RadioGroup中也可以包含RadioButton之外的其他控件。

8.3.2.使用滚动条

当屏幕上控件的内容超过屏幕本身的尺寸时,一般可以通过出现滚动条(ScrollBar)供用户拖动来显示没有显示的内容。Android 使用滚动视图(ScrollView)来支持滚动条。

参考示例程序:ScrollView(ApiDemo=>Views=>ScrollView=>各个程序) 源代码:

代码语言:javascript
复制
com/example/android/apis/view/ScrollBar1.java 
com/example/android/apis/view/ScrollBar2.java 
com/example/android/apis/view/ScrollBar3.java 

布局文件:scrollbar1.xml、scrollbar2.xml 和 scrollbar3.xml ScrollView 相关的程序的运行结果如图所示:

在这里插入图片描述
在这里插入图片描述

scrollbar1.xml 的内容如下所示:

代码语言:javascript
复制
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <LinearLayout 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <LinearLayout 
 android:id="@+id/layout" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" android:layout_height="wrap_content"> 
 <TextView 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="@string/scrollbar_1_text"/> 
 <TextView 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="@string/scrollbar_1_text"/> 
 </LinearLayout> 
</ScrollView>

在 scrollbar2.xml 和 scrollbar3.xml 文件的内容也与之类似。 ScrollView 类的扩展关系如下所示:

代码语言:javascript
复制
 => android.view.ViewGroup 
 => android.widget.FrameLayout 
 => android.widget.ScrollView 

ScrollView 类通常在 XML 文件中使用,当屏幕上的内容预计超过屏幕尺寸时,用一个 ScrollView 将其他内容包含起来,这样就可以出现滚动条。

参考示例程序:ScrollView(Views=>Layout=>ScrollView=>2) 源代码:com/example/android/apis/view/ScrollView2.java 布局文件:scroll_view_2.xml ScrollView2 程序的运行结果如图所示:

在这里插入图片描述
在这里插入图片描述

scroll_view_2.xml 布局文件的内容如下所示:

代码语言:javascript
复制
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" android:layout_height="wrap_content" 
 android:scrollbars="none"> 
 <LinearLayout 
 android:id="@+id/layout" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <TextView 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="@string/scroll_view_2_text_1"/> 
 <Button 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="@string/scroll_view_2_button_1"/> 
 </LinearLayout> 
</ScrollView>

这里指定了 android:scrollbars="none"表示本屏幕中没有滚动杆,即使这样依然可以使用上下键和触摸屏进行上下移动。 源文件 ScrollView2.java 中的主要内容如下所示:

代码语言:javascript
复制
    public class ScrollView2 extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.scroll_view_2);
            LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
            for (int i = 2; i < 64; i++) {
                TextView textView = new TextView(this);
                textView.setText("Text View " + i);
                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                );
                layout.addView(textView, p);
                Button buttonView = new Button(this);
                buttonView.setText("Button " + i);
                layout.addView(buttonView, p);
            }
        }
    }

在这里是直接获得了 LinearLayout 的句柄,在其中用循环的方式增加了若干组(2-64)文本框和按钮,这样就形成了一个在界面上的长列表。本例子的第一组文本框和按钮是在布局文件中指定的,其他是在代码中指定的。 Android 应用虽然支持滚动视图,但是在手机上,一般的界面并不一定适合使用这种的方式,在大多数情况下还是应该协调屏幕的尺寸和元素,保证一个屏幕可以完全显示内容。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/01/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 8.3 作为简单容器使用的视图组
    • 8.3.1.单选按钮组
      • 8.3.2.使用滚动条
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档