天天看点

android中布局及ui组件中的注意点

这两天一直在看有关布局,组件的相关知识,这一块感觉知识点很散,毕竟每个布局都有一些各自需要记忆的,但总的还是相似。我个人觉得对付这一块,一是要有耐心,要不断去修改,最后可能才会得到理想的效果。二是要尽可能多的手敲代码,去看这段在手机上显示的真正效果如何,多看多记,像我之前曾提到过的,图像总是更直观,印象更加深刻。下面提到的这些是这2天学习中我遇到的一些问题和易混知识点:

1.TableLayout,GridLayout:表格布局与网格布局,前者在布局定义时不需要声明是多少行,多少列,我们往里面直接加一个控件,它就可以是一行,但如果我们往TableRow中加,则直到TableRow声明结束,其中所包含的组件都在一行中。网格布局则一开始要指明多少行,多少列。这里有两个名字很容易混的地方,在GridLayout中,带有span后缀的是指横跨这些行列,而GridLayout.Spec是一个类,我们可以通过这个类来在网格布局中定位我们想要放置的组件的位置,列如一个计算器界面中的定位,部分代码如下:

android中布局及ui组件中的注意点

2.在TextView中有一个属性singleline,是指内容全都在一行,如果此时内容较多,我们可以加上ellipsize属性,它是控制内容省略的,但是当我们用singleline时,可以不用ellipsize,但当我们用ellipsize时,必用singleline。

3.归纳几个绘制textview边框时的标签,我们必须将边框属性的xml文件置于drawble/下,然后就可以把这个xml文档当做图片来用了。

<stroke>可在这个标签中绘制边框的大小,颜色等。

<gradient>可在这个标签中绘制背景颜色的渐变,它的英文翻译本身就是阶梯度。

<corners>可在这个标签中绘制边框的四个角呈现圆形边角。

<shape>上述几个标签都是被包在这个标签中的。

android中布局及ui组件中的注意点

4.在RadioGroup中,当某一选项设置了checked="true";后,最好给这个RadioGroup中的每一个选项都赋予一个id值,这在RadioGroup的OncheckedchangLIstener的onCheckedChanged()方法中对选项id判别时十分有用,实战教训告诉我们,真的要加啊

android中布局及ui组件中的注意点

5.RadioButtun,toggleButtun,switch:

这三个其实非常相似,都是通过是否判断是否被勾选了,或者我了解来说,就是现在的状态与之前的状态不一样了,来对值及表现形式作出改变。他们都是通过OnCheckedChangeListen中的onCheckedChanged()方法来进行判断,但所引用的类及方法参数的类型却不一样,请注意红色部分!

RadioButton:

android中布局及ui组件中的注意点

ToggleButton/Switch:

android中布局及ui组件中的注意点

这里多提一下ToggleButtun与Switch,触发它们的是状态的改变,而这个改变与我们的动作无关,只与它们感知到的状态改变有关。额,这句话听起来很抽象,下面有一段代码,可以复制后试试,它们最后在手机上的展示效果是这样的,

android中布局及ui组件中的注意点

你会发现一个问题,当我们点击纵向排列后,下面三个按钮会变成横向排列,

android中布局及ui组件中的注意点

此时Switch开关在我们手并没有接触到它的状态下,会自动滑向另一边,原因就像我上面说的那样,它们只感知状态的改变,与手势无关。(代码在文末)

7.在TextClock的显示中,提到了这样一行代码,android:format12Hour="yyyy年MM月dd日 H:mma EEEE";通过这样我们可以得到列如“2016年4月7日 7:30pm 星期四”这种格式的时间,但是不知为什么,在我的手机上运行时,只会出现0:53,除此外,没有任何信息,在网上搜了,也没找到原因,后来自己在那乱试时,发现这样一行,可以实现l例如“2016年4月7日 7:30下午 星期四”:

android:format24Hour="yyyy年MM月dd日 H:mma EEEE";(有木有感觉很错乱

android中布局及ui组件中的注意点

)。

8.一张图,可以很清晰的看出TextView以及哪些是它的子类:

android中布局及ui组件中的注意点

附上ToggleButton与Switch实现上述操作的代码:

XML文件,java文件;

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

    <ToggleButton android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="横向排列"
        android:textOn="纵向排列"
        android:checked="true"/>
    <Switch android:id="@+id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="横向排列"
        android:textOn="纵向排列"
        android:checked="true"
        android:thumb="@drawable/monkey"/>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/test">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮一"/>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮二"/>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮三"/>
        </LinearLayout>
</LinearLayout>
      

j

package com.example.layout;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;

public class ToggleSwitch extends AppCompatActivity {
  ToggleButton tg;
    Switch aSwitch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_toggle_switch);
        tg=(ToggleButton)findViewById(R.id.toggle);
        aSwitch=(Switch)findViewById(R.id.switcher);
        final LinearLayout test=(LinearLayout)findViewById(R.id.test);
        CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    test.setOrientation(1);
                }
                else{
                    test.setOrientation(0);
                }
            }
        };
        tg.setOnCheckedChangeListener(listener);
        aSwitch.setOnCheckedChangeListener(listener);
    }

}
      

android:textOn="纵向排列"

        android:checked="true"

        android:thumb="@drawable/monkey"/>

    <LinearLayout android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        android:id="@+id/test">

        <Button android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="按钮一"/>

        <Button android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="按钮二"/>

        <Button android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="按钮三"/>

        </LinearLayout>

</LinearLayout>