zoukankan      html  css  js  c++  java
  • 学习总结

       经过一个月的Android学习,让我学会了很多关于Android的知识。在刚开始学习时,进行的第一个Android课后作业就是界面布局,而在制作这个布局文件中,首先我从书上知道了

    在android中我们常用的布局方式有这么几种:LinearLayout (线性布局),RelativeLayout (相对布局),TableLayout (表格布局),AbsoluteLayout (绝对布局),FrameLayout (帧布局)。

    LinearLayout 和 RelativeLayout 应该又是其中用的较多的两种。AbsoluteLayout 比较少用(我自己还没用过),因为它是按屏幕的绝对位置来布局的如果屏幕大小发生改变的话控件的位置也发生了改变。这个就相当于HTML中的绝对布局一样,一般不推荐使用。

    LinearLayout 顾名思义就是一条条的将控件布置下去,线性布局分为水平线性和垂直线性二者的属性分别为

    android:orientation="horizontal"   android:orientation="vertical" 。xmlns:android="http://schemas.android.com/apk/res/android" 很多人对这个感到困惑,其实它就是一个命名空间。

    RelativeLayout 相对布局。里面的每个控件之间的关系都是相对的。如果不设置相对关系的话默认摆放在屏幕左上角。重要属性如下:

    android:layout_toRightOf="@id/city" :与id为city的控件的右边对齐。

    android:layout_alignTop="@id/city"  : 与id为city的控件的顶部对齐。

    给出示意图如下所示:

    android:layout_width="fill_parent" android:layout_height="wrap_content"  我一般在线性布局里面套相对布局,这时候需要注意上面两个属性相对布局的宽度可以设置为填充父控件,但是高度一般不要设置为填充父控件因为这样的话我想在相对布局外面在放控件就没有效果了因为屏幕已经被相对布局全部占据。

    不管是用什么布局,宽度与高度这两个属性一定要弄清楚刚开始的时候我经常会发现有些控件没显示在界面上或者占据了整个屏幕,一检查原来是这里设置错了。还有不要忘记这两个属性在一般控件中都是不可或缺的,忘记设置的话就会报错。

       界面控件的基本结构有哪些基本常用的界面控件有:TextView:显示文本信息,而在一个简单的登录界面布局文件中需要有TextView,EditText,Button三个控件才能完成布局。对于Textview,EditView需要几个必须的属性 ,通过下面的部分代码说明

    1.对于TextView作用是显示文本信息,在使用时需要设置android:layout_width,android:layout_heigh前两个分别是设置控件的宽度和高度;android:id设置组件的ID,用于在代码中引用,android:text设置文本内容,android:textSize设置文本大小;android:layout_gravity设置控件相对其所在容器的位置。在TextView中必须要有这些属性,你也可以添加一些属性,丰富你的界面。

    2.对于EditView大部分和TextView有相同的属性,但是Android:hint不同于Android:text。Android:hint表示的属性是设置编辑框内容为空时显示的提示信息,当输入密码时会消失,而Android:Text不会。

     1    <TextView
     2     android:layout_width="match_parent"
     3     android:layout_height="50dp"
     4     android:background="#DBDBDB"
     5     android:gravity="center"
     6     android:text="登陆界面"
     7     android:textSize="22sp" />
     8     <EditText
     9        android:id="@+id/ev_userName"
    10         android:layout_width="match_parent"
    11         android:layout_height="wrap_content"
    12         android:hint="请输入用户名:"  />

     三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便。

    1)fill_parent

    设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。

    2) wrap_content

    设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

    3)match_parent    Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了


    对于ImageView控件使用最多的属性是Android:src ,用于设置ImageView中展示什么图片。其他的属性和EditView和TextView大部分相同

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="100sp"
            android:id="@+id/page"
            android:scaleType="center"
            android:adjustViewBounds="false"
            android:cropToPadding="false"
            android:src="@mipmap/map"
            android:layout_marginTop="50dp" />

     

    想要如图所示的界面,需要在XML文件中设置几个属性ImageView,RadioButton控件,RadioGroup是单选组合框,它用于将RadioButton框起来,使得RadioButton只能选择一个,实现单选效果。

    部分代码:

     1 <RadioGroup
     2             android:layout_width="match_parent"
     3             android:layout_height="58dp"
     4             android:id="@+id/rg1"
     5             android:orientation="horizontal" >
     6 
     7             <RadioButton
     8                 android:text="梅花"
     9                 android:layout_width="wrap_content"
    10                 android:layout_height="wrap_content"
    11                 android:id="@+id/rb8"
    12                 android:textSize="25dp" />
    13 
    14             <RadioButton
    15                 android:text="石楠花"
    16                 android:layout_width="wrap_content"
    17                 android:layout_height="wrap_content"
    18                 android:id="@+id/rb10"
    19                 android:textSize="25dp" />
    20 
    21             <RadioButton
    22                 android:text="象牙花"
    23                 android:layout_width="wrap_content"
    24                 android:layout_height="wrap_content"
    25                 android:id="@+id/rb9"
    26                 android:textSize="25dp"
    27                 android:layout_weight="1" />
    28         </RadioGroup>

    这是一组RadioGroup,可以实现对上面的花朵进行单选设置。

    在编译中,一般使用RadioGroup的getCheckedRadioButtonId方法来获取RadioGroup中具体哪一个RadioButton被选中。

    对于CheckBox和Button一样,是一种常见的控件,是一个带有选中或不选中状态的按钮,既可以用于多选,也可以用于单选。

    完成界面的编写后,继续写Java文件,使得程序能够应用。

    1.定义控件

     private ImageView page;
        private RadioGroup rg1;
        private RadioGroup rg2;

    对于page,rg1,rg2有人可能会认为必须和XML文件中的ID相同,其实不是,page等只是定义的控件名,并不是ID。

    2.获取控件

    1  page = (ImageView) findViewById(R.id.page);
    2         rg1 = (RadioGroup) findViewById(R.id.rg1);
    3         rg2 = (RadioGroup) findViewById(R.id.rg2);

    findViewById(R.id.page)括号中的page才是ID
    3.设置监听事件,并添加所要实现的功能。

     rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {   //监听rgrg RadioGroup组
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    if (rb8.isChecked()) {
                        page.setImageResource(R.drawable.map);//用于显示自己添加的图片
                        rg2.clearCheck();// 用于显示单选,不出现多选的情况
                    }

    学习过程中所遇到的问题及解决方法
    1.Java文件中的R全部显示错误

    一般可以通过Androidstudio的状况栏中的build->clear program或rebuild program恢复。

    可能是布局文件哪里出错或Java文件代码写错,但是没有报错,可以通过logcat查看错误。

    2.程序代码没有显示错误,但是不能运行,出现闪退情况。

    没有代码错误,可能是String.xml文件出现错误,没有定义组件。

    3.模拟器不能使用

    把模拟器删除,在重新下载一个。

     
  • 相关阅读:
    log4j日志基本配置
    MyBatis基本应用
    Java properties配置文件
    Java DAO模式
    Java方式 MySQL数据库连接
    bug 复制代码没有审查,没有完全就该变量名
    cocos3 深入理解tiledmap
    cocos3 深入理解单例模式
    cocos3 CC_BREAK_IF(m_pGameMap==NULL);
    cocos2d3 宏定义屏幕宽高,这样就不用重复获取了
  • 原文地址:https://www.cnblogs.com/zhoushasha/p/6617198.html
Copyright © 2011-2022 走看看