美文网首页
Android编程权威指南 - 第8章 使用布局与组件创建用户界

Android编程权威指南 - 第8章 使用布局与组件创建用户界

作者: JMasche | 来源:发表于2016-11-22 01:40 被阅读61次

    新界面代码

    更新fragment_crime.xml布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              
          android:layout_width="match_parent"              
          android:layout_height="wrap_content"    
          android:orientation="vertical">        
          <TextView        
                android:layout_width="match_parent"        
                android:layout_height="match_parent"        
                android:text="@string/crime_title_label"        
                style="?android:listSeparatorTextViewStyle"        
                />    
          <EditText        
                android:id="@+id/crime_title"        
                android:layout_width="match_parent"        
                android:layout_height="wrap_content"        
                android:layout_marginLeft="16dp"        
                android:layout_marginRight="16dp"        
                android:hint="@string/crime_title_hint" />    
          <TextView        
                android:layout_width="match_parent"        
                android:layout_height="wrap_content"        
                android:text="@string/crime_details_label"        
                style="?android:listSeparatorTextViewStyle"        
                />        
          <Button        
                android:id="@+id/crime_date"        
                android:layout_width="match_parent"        
                android:layout_height="wrap_content"        
                android:layout_marginLeft="16dp"        
                android:layout_marginRight="16dp" />    
          <CheckBox        
                android:id="@+id/crime_solved"        
                android:layout_width="match_parent"        
                android:layout_height="wrap_content"        
                android:layout_marginLeft="16dp"        
                android:layout_marginRight="16dp"        
                android:text="@string/crime_solved_label"/>
    </LinearLayout>
    
    • 增加一个TextView组件,用了一个新的标签style
    • 增加了一个Button组件,用了两个新的标签android:marginLeftandroid:marginRight
    • 增加一个新的组件CheckBox

    更新CrimeFragment.java

    • 新增Button支撑代码,主要是设置Button的text内容
    mDateButton = (Button)v.findViewById(R.id.crime_date);
    mDateButton.setText(mCrime.getDate().toString());
    mDateButton.setEnabled(false);
    
    • 新增CheckBox支撑代码。
      CheckBox组件的监听事件是setOnCheckedChangeListener,复写onCheckedChanged方法。
      CompoundButtonandroid.widget.CompoundButton
    mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){    
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        
                mCrime.setSolved(isChecked);    
          }
    });
    

    深入探讨XML布局属性

    样式、主题及主题属性

    • 样式是XML资源文件,含有用来描述组件行为和外观的属性定义。
      如下面的例子
    <style name="BigTextStyle">
          <item name="android:textSize">20sp</item>
          <item name="android:layout_margin">3dp</item>
    </style>
    
    • 可以创建自己的样式文件,并保存在res/values/目录下,然后在布局文件中以@style/my_own_style(样式文件名)的形式引用
    • 在上面fragment_crime.xml布局文件的例子中,TextView标签的style属性,引用了Android平台自带的主题。

    dp、sp以及屏幕像素密度

    • 视图属性指定大小尺寸值
      • text size文字大小:指设备上显示的文字像素高度
      • margin边距:指定视图组件间的距离
      • padding内边距:指定视图外边框与其内容间的距离
    • Android提供了密度无关的尺寸单位,这种单位可以在不同屏幕密度的设备上获得同样大小的尺寸。
      • dp
        density- independent pixel,意为密度无关的像素。使用dp的好处是,无论屏幕密度如何,总能获得相同尺寸。在 设置边距、内边距或任何不打算按像素值指定尺寸的情况下,通常都使用dp这种单位。
      • sp
        scale- independent pixel,意为缩放无关像素。与像素无关,但是受字体偏好设置影响。通常会使用sp来设置文字大小。
      • pt、mm、in
        类似于dp的缩放单位。允许以点( 1/ 72 英寸)、毫米或英寸为单位指定用户界面尺寸。实际开发中并不推荐使用。

    LinearLayout标签内容

    • android:orientation属性,如果值为vertical,则包含在里面的控件是竖着摆放;如果值为horizontal,则包含在里面的控件是横着摆放。如果不填写,则值默认为horizontal。
    • 布局的工作原理如下:
      1. 如果是horizontal,则先看layout_width属性;如果是vertical,则先看layout_heigth属性
      2. 假设是horizontal,再看layout_weigth属性值。如果相同则是均分;如果一个为2,一个为1,则是2:1的比例分成。
      3. 如果layout_width="0dp",则只考虑layout_weigth属性值

    相关文章

      网友评论

          本文标题:Android编程权威指南 - 第8章 使用布局与组件创建用户界

          本文链接:https://www.haomeiwen.com/subject/ymhppttx.html