TextView

先贴API网址

  • 什么是dp(dip), px, pt, sp

dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。 px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。 pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用; sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。

  • TextView属性详解, 解释如下代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:gravity="center"
  7. android:background="#8fffad">
  8. <TextView
  9. android:id="@+id/txtOne" what, and why
  10. android:layout_width="200dp"
  11. android:layout_height="200dp"
  12. android:gravity="center"
  13. android:text="TextView(显示框)"
  14. android:textColor="#EA5246"
  15. android:textStyle="bold|italic"
  16. android:background="#000000"
  17. android:textSize="18sp" />
  18. </RelativeLayout>
  • 如何做带阴影的TextView

android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用哦!
android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置

  • 使用autolink识别连接类型
  • 代码实现单行显示跑马灯效果:
    1. <TextView
    2. android:id="@+id/txtOne"
    3. android:layout_width="match_parent"
    4. android:layout_height="wrap_content"
    5. android:textSize="18sp"
    6. android:singleLine="true"
    7. android:ellipsize="marquee"
    8. android:marqueeRepeatLimit="marquee_forever"
    9. android:focusable="true"
    10. android:focusableInTouchMode="true"
    11. android:text="你整天说着日了狗日了狗,但是你却没有来,呵呵呵呵呵呵呵呵呵呵~"/>

EditText

  • 如何设置默认提示文本?

    1. android:hint="默认提示文本"
    2. android:textColorHint="#95A1AA"
  • 如何设置点击后自动选中全部文本?

android:selectAllOnFocus=”true”

  • 如何限制EditText输入的类型?
    1. <EditText
    2. android:layout_width="fill_parent"
    3. android:layout_height="wrap_content"
    4. android:inputType="phone" />

带边框的文本框

编写一个ShapeDrawable的资源文件!然后TextView将blackgroung 设置为这个drawable资源即可!
简单说下shapeDrawable资源文件的几个节点以及属性:

  • <solid android:color = “xxx”> 这个是设置背景颜色的
  • <stroke android:width = “xdp” android:color=”xxx”> 这个是设置边框的粗细,以及边框颜色的
  • <padding androidLbottom = “xdp”…> 这个是设置边距的
  • <corners android:topLeftRadius=”10px”…> 这个是设置圆角的
  • <gradient> 这个是设置渐变色的,可选属性有: startColor:起始颜色 endColor:结束颜色 centerColor:中间颜色 angle:方向角度,等于0时,从左到右,然后逆时针方向转,当angle = 90度时从下往上 type:设置渐变的类型

使用drawablexxx设置文本旁边带图片

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="com.jay.example.test.MainActivity" >
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_centerInParent="true"
  10. android:drawableTop="@drawable/show1"
  11. android:drawableLeft="@drawable/show1"
  12. android:drawableRight="@drawable/show1"
  13. android:drawableBottom="@drawable/show1"
  14. android:drawablePadding="10dp"
  15. android:text="张全蛋" />
  16. </RelativeLayout>

Button按钮

ImageView

  • 作用? 显示图像的控件
  • src与background的区别

①background通常指的都是背景,而src指的是内容!!
②当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸
而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸

  • 如何设置图像缩放时是否按照长宽比?

ImageView为我们提供了adjustViewBounds属性,用于设置缩放时是否保持原图长宽比! 单独设置不起作用,需要配合maxWidthmaxHeight属性一起使用!而后面这两个属性 也是需要adjustViewBounds为true才会生效的~

  • android:maxHeight:设置ImageView的最大高度
  • android:maxWidth:设置ImageView的最大宽度

RadioButton and CheckBox使用

How? 先设置RadioGroup ,在<radioGroup 里面设置RadioButton. RadioGroup设置button的排列方式和相对大小

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity" >
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="请选择性别"
  12. android:textSize="23dp"
  13. />
  14. <RadioGroup
  15. android:id="@+id/radioGroup"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:orientation="horizontal">
  19. <RadioButton
  20. android:id="@+id/btnMan"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="男"
  24. android:checked="true"/> //设置默认选中
  25. <RadioButton
  26. android:id="@+id/btnWoman"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="女"/>
  30. </RadioGroup>
  31. <Button
  32. android:id="@+id/btnpost"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="提交"/>
  36. </LinearLayout>
  1. RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
  2. //第一种获得单选按钮值的方法
  3. //为radioGroup设置一个监听器:setOnCheckedChanged() 只要button受到点击就会触发行为
  4. radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  5. @Override
  6. public void onCheckedChanged(RadioGroup group, int checkedId) {
  7. RadioButton radbtn = (RadioButton) findViewById(checkedId);
  8. Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
  9. }
  10. });

第二种方法:提交后告诉用户他选取的值:

  1. Button btnchange = (Button) findViewById(R.id.btnpost); //提交按钮。
  2. RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
  3. //为radioGroup设置一个监听器:setOnCheckedChanged()
  4. btnchange.setOnClickListener(new OnClickListener() {
  5. @Override
  6. public void onClick(View v) {
  7. for (int i = 0; i < radgroup.getChildCount(); i++) { //getChildCount()
  8. RadioButton rd = (RadioButton) radgroup.getChildAt(i); //getChildAt()
  9. if (rd.isChecked()) { //isChecked()
  10. Toast.makeText(getApplicationContext(), "点击提交按钮,获取你选择的是:" + rd.getText(), Toast.LENGTH_LONG).show();
  11. break;
  12. }
  13. }
  14. }
  15. });

开关按钮ToggleButton 和开关Switch

  • ToggleButton的属性

    • disabledAlpha
    • textOff
    • textOn
  • Switch

    • showText
    • splitTrack
    • textOff
    • textOn
    • thumb 滑块的图片
    • track是底部的图片

Progress bar

具有如下属性:

  • android:max:进度条的最大值
  • android:progress:进度条已完成进度值
  • android:progressDrawable:设置轨道对应的Drawable对象
  • android:indeterminate:如果设置成true,则进度条不精确显示进度
  • android:indeterminateDrawable:设置不显示进度的进度条的Drawable对象
  • android:indeterminateDuration:设置不精确显示进度的持续时间
  • android:secondaryProgress:二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过progress属性进行设置!

对应的再Java中我们可调用下述方法:

  • getMax():返回这个进度条的范围的上限
  • getProgress():返回进度
  • getSecondaryProgress():返回次要进度
  • incrementProgressBy(int diff):指定增加的进度
  • isIndeterminate():指示进度条是否在不确定模式下
  • setIndeterminate(boolean indeterminate):设置不确定模式下

SeekBar 拖动条

  • 属性

    • max
    • progress
    • secondaryProgress
    • thumb
  • 接着要说下SeekBar的事件了,SeekBar.OnSeekBarChangeListener 我们只需重写三个对应的方法:

    onProgressChanged:进度发生改变时会触发 onStartTrackingTouch:按住SeekBar时会触发 onStopTrackingTouch:放开SeekBar时触发

RatingBar

  • 属性:

android:isIndicator:是否用作指示,用户无法更改,默认false
android:numStars:显示多少个星星,必须为整数
android:rating:默认评分值,必须为浮点数
android:stepSize: 评分每次增加的值,必须为浮点数
除了上面这些,还有两种样式供我们选择咧,但是不建议使用,因为这两种样式都好丑… 他们分别是:
style=”?android:attr/ratingBarStyleSmall”
style=”?android:attr/ratingBarStyleIndicator”

  • 事件处理:

只需为RatingBar设置OnRatingBarChangeListener事件,然后重写下onRatingChanged()方法即可!

ScrollView滚动条

嘿嘿,原来是一个FrameLayout的容器,不过在他的基础上添加了滚动,允许显示的比实际多的内容!
另外,只能够往里面放置一个子元素,可以是单一的组件,又或者一个布局包裹着的复杂的层次结构!
一般对于可能显示不完的情况,我们可以直接在布局的外层套上一个: ScrollView或者HorizontalScrollView!就这么简单~!

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private Button btn_down;
  3. private Button btn_up;
  4. private ScrollView scrollView;
  5. private TextView txt_show;
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. bindViews();
  11. }
  12. private void bindViews() {
  13. btn_down = (Button) findViewById(R.id.btn_down);
  14. btn_up = (Button) findViewById(R.id.btn_up);
  15. scrollView = (ScrollView) findViewById(R.id.scrollView);
  16. txt_show = (TextView) findViewById(R.id.txt_show);
  17. btn_down.setOnClickListener(this);
  18. btn_up.setOnClickListener(this);
  19. StringBuilder sb = new StringBuilder();
  20. for (int i = 1; i <= 100; i++) {
  21. sb.append("呵呵 * " + i + "\n");
  22. }
  23. txt_show.setText(sb.toString());
  24. }
  25. @Override
  26. public void onClick(View v) {
  27. switch (v.getId()) {
  28. case R.id.btn_down:
  29. scrollView.fullScroll(ScrollView.FOCUS_DOWN);
  30. break;
  31. case R.id.btn_up:
  32. scrollView.fullScroll(ScrollView.FOCUS_UP);
  33. break;
  34. }
  35. }
  36. }

Date and Time

  • TextClock 文本时钟
    • format12Hour
    • format24Hour
    • timeZone
  • AnalogClock 模拟时钟
  • Chronometer 计时器
  • DatePicker
    • 事件:
      • DatePicker.OnDateChangedListener
  • TimePicker
    • 属性只有一个:timePickerMode
    • 事件:TimePicker.OnTimeChangedListener
  • CalendarIView