作者:夏至  欢迎转载,也请保留这段申明,谢谢    

1. ImageView(图像显示

    ImageView 呢,是图像按钮,我们可以用它来显示我们想要显示的图像。 其中最主要的属性就是 android:scaleType = ?  这里有两个属性
    · center  就是按照一定的比例来裁剪缩放
    · fitcenter 就是按照我们设定好的内容来缩放,默认是采用这个模式

    我们先定义两个 ImageView 控件来显示不一样的效果
  1. <ImageView
  2. android:layout_width="75dp"
  3. android:layout_height="64dp"
  4. android:scaleType="fitCenter"
  5. android:src="@drawable/image5"/>
  6. <ImageView
  7. android:id="@+id/imageDemo"
  8. android:layout_width="75dp"
  9. android:layout_height="64dp"
  10. android:scaleType="center"
  11. android:src="@drawable/image5"/>

效果图:


可以看到第一个图片按照我们的压缩比例缩放,第二张则是裁剪了中间部分。当然你也可以在activity 实现


当我们使用center属性时,我们可以用主activity来实现裁剪
  1. public class ImageViewDemo extends Activity{
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.imageview);
  6. ImageView imageView = (ImageView)findViewById(R.id.imageDemo);
  7. imageView.setLayoutParams(new LinearLayout.LayoutParams(300,300));
  8. }
  9. }
效果如下:



2. RadioButton (单击按钮)
    如题所示,单击按钮只能选中一次,如上图所示,我们在做调查问卷的时候,经常用到。而这个功能的实现呢,我们用radiogroup和radiobutton实现。把radiobutton内层放到radiogroup外层里,然后设置好外层的对齐方式即可。外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平~。布局文件如下:
  1. <TextView
  2.     android:id="@+id/text"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="wrap_content"
  5.     android:text="使用单击按钮"
  6.     android:textSize="24sp"
  7. />
  8. <RadioGroup
  9.     android:id="@+id/group"
  10.     android:layout_width="wrap_content"
  11.     android:layout_height="wrap_content"
  12.     android:orientation="horizontal">
  13. <RadioButton
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:text="男"
  17.         android:checked="true"
  18.     />
  19. <RadioButton
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:text="女"
  23.         android:checked="true"
  24.     />

ok,layout层的程序我们已经写好,那么,当我们选择的时候,怎么提示呢?我们用toast来显示里我们可以在主函数中来判断。

如:
  1. RadioGroup radioGroup = (RadioGroup) findViewById(R.id.group);
  2. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  3. @Override
  4. public void onCheckedChanged(RadioGroup group, int checkedId) {
  5. RadioButton radioButton = (RadioButton) findViewById(checkedId); //这里用checkedId,来遍历
  6. Toast.makeText(MainActivity.this, "按钮组值发生改变,你选了" +
  7. radioButton.getText(), Toast.LENGTH_SHORT).show();


但一般我们不这样来显示,我们一般用一个按键来对选择的内容进行提示,所以这里,我们还要在layout层添加一个button组件
  1. <Button
  2.     android:id="@+id/btn"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:text="确定"
  6.     android:textSize="24sp"
  7. />

在主activity那里,我们要写button的事件函数
  1.     ......
  2.     radioGroup = (RadioGroup) findViewById(R.id.group);
  3.     Button button = (Button)findViewById(R.id.btn);
  4.     button.setOnClickListener(this);
  5. }
  6. @Override
  7. public void onClick(View v) {
  8.     // radioGroup.getChildCount()获得按钮组中的单选按钮的数目;
  9.     for (int i = 0;i<radioGroup.getChildCount();i++){
  10.     //radioGroup.getChildAt(i) 根据索引值获取我们的单选按钮
  11.     RadioButton rad = (RadioButton)radioGroup.getChildAt(i);
  12. if(rad.isChecked()){ // isChecked()判断按钮是否选中
  13.             Toast.makeText(MainActivity.this,"你选择的性别是: "+rad.getText(),
  14.             Toast.LENGTH_SHORT).show();
  15. break;

效果如图:
    


3.CheckBox(复选框)

    如题所示,这个是我们的复选框,那么我们在多项选择的时候,是经常性地应用到。那么它究竟张什么样呢

没错,那么我们现在就在layout上添加我们的程序吧
  1. <TextView
  2.     android:layout_width="wrap_content"
  3.     android:layout_height="wrap_content"
  4.     android:textScaleX="24sp"
  5.     android:text="请选择你喜欢的水果"
  6. />
  7. <CheckBox
  8.     android:id="@+id/one"
  9.     android:layout_width="wrap_content"
  10.     android:layout_height="wrap_content"
  11.     android:text="香蕉"
  12. />
  13. <CheckBox
  14.     android:id="@+id/two"
  15.     android:layout_width="wrap_content"
  16.     android:layout_height="wrap_content"
  17.     android:text="苹果"
  18. />
  19. <CheckBox
  20.     android:id="@+id/three"
  21.     android:layout_width="wrap_content"
  22.     android:layout_height="wrap_content"
  23.     android:text="菠萝"
  24. />
  25. <Button
  26.     android:id="@+id/btn"
  27.     android:layout_width="wrap_content"
  28.     android:layout_height="wrap_content"
  29.     android:text="提交"
  30. />

在调用上,我们采用第二个单选的方法,让一个button来确定选中了多少个
  1. ....
  2. button = (Button)findViewById(R.id.btn);
  3. one = (CheckBox)findViewById(R.id.one);
  4. two = (CheckBox)findViewById(R.id.two);
  5. three = (CheckBox)findViewById(R.id.three);
  6. button.setOnClickListener(this);
  7. }
  8. @Override
  9. public void onClick(View v) {
  10. String choose = "";
  11. if(one.isChecked())
  12. choose += one.getText().toString();
  13. if(two.isChecked())
  14. choose += two.getText().toString();
  15. if(three.isChecked())
  16. choose += three.getText().toString();
  17. Toast.makeText(MainActivity.this,choose,Toast.LENGTH_SHORT).show();
  18. }
效果如图:

如有错误,欢迎指出,如果喜欢,欢迎收藏!