0
点赞
收藏
分享

微信扫一扫

Android常用控件技巧总结


1.圆角按钮:

shape.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <!-- 填充的颜色 --> 
    <solid android:color="#FFFFFF" /> 
    <!-- 设置按钮的四个角为弧形 --> 
    <!-- android:radius 弧形的半径 --> 
    <corners android:radius="5dip" /> 
      
<!-- padding:Button里面的文字与Button边界的间隔 --> 
<padding 
   android:left="10dp" 
   android:top="10dp" 
   android:right="10dp" 
   android:bottom="10dp" 
/> 
</shape>

 ------------------------------------------------------------

<Button   
    android:id="@+id/roundButton" 
    android:text=" 圆角按钮 " 
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:background="@drawable/shape" 
    />

 

2.圆角输入框:

input_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" android:padding="10dp">
 <solid android:color="#FFFFFF" /><stroke android:width="1dp" android:color="#808080"/>
 <corners android:bottomRightRadius="15dp"
  android:bottomLeftRadius="15dp" android:topLeftRadius="15dp"
  android:topRightRadius="15dp" />
</shape>

---------------------------------------------------------------

<EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/input_shape"
                android:paddingBottom="10dip"
                android:paddingLeft="50dip"
                android:paddingTop="10dip"
                android:text="请输入您的用户名"
                android:textColor="#FFFAFA" />

3.图片缩小:

<ImageView android:id="@+id/img" 
    android:src="@drawable/logo"
    android:scaleType="centerInside"
    android:layout_width="60dip"
    android:layout_height="60dip"
    android:layout_centerVertical="true"/>

4.TextView太长添加滚动条:

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
 
         <TextView
             android:id="@+id/text"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textSize="20sp"
             android:text="TextView" />      
</ScrollView>

 

5.除去页面标题栏:

<activity android:name=".LoadingSystem" android:label="@string/app_name"  android:theme="@android:style/Theme.NoTitleBar">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN"/>  
                <category android:name="android.intent.category.LAUNCHER"/>     
            </intent-filter>  
        </activity>

6.取消EditText自动获取焦点行为:

在EditText的父级控件添加:

android:focusable="true"  
android:focusableInTouchMode="true"

7.Android控件的显示与隐藏:

setVisible(VIew.GONE);//隐藏 并且不占用界面空间
setVisible(VIew.VISIBLE);//控件显示
setVisible(VIew.INVISIBLE);//控件隐藏 占用界面空间

8.单选框:

<RadioGroup
                    android:id="@+id/gendergroup"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >                    <RadioButton
                        android:id="@+id/girl"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/girl" />                    <RadioButton
                        android:id="@+id/boy"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/boy" />
                </RadioGroup>

 

9.让listView显示的数据靠下方显示:

android:stackFromBottom="true"

10.android设置字体颜色:

tv.setTextColor(this.getResources().getColor(R.color.textColor_black));

 

11.TextView过长设置显示省略号:

android:ellipsize="end"
  android:singleLine="false"
  android:maxLines="2"

12.在代码中设置ImageView的src属性:

setImageDrawable(drawable);
setImageBitmap(bm);
setImageResource(resId);pic1.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.add_element_img));

13.Drawable与Bitmap的相互转换

//Drawable--->Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
    return ((BitmapDrawable)drawable).getBitmap();
}
//Bitmap--->Drawable
public static Drawable bitmapToDrawable(Bitmap bitmap){
    return new BitmapDrawable(bitmap);
}

 

14.设置ListView数据靠下方显示:

android:stackFromBottom="true"

 

15.取消ListView分割线:

<ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@null" >
</ListView>

 

16.Android 设置界面全屏显示:

<activity 
            android:name="com.weijie.user.activity.OrderDetailActivity"
            android:launchMode="singleTask"
            android:configChanges="orientation|keyboardHidden"
            android:windowSoftInputMode="stateAlwaysHidden"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        </activity>

17.Android使用html设置TextView颜色:

this.pic_tip = (TextView) this.findViewById(R.id.pic_tip);
  String tipInfo = "(<font color='red'>*</font> 请上传本人手持身份证的近照)";
  pic_tip.setText(Html.fromHtml(tipInfo));

18.Android TextView(EditView)文字底部或者中间 加横线

tv = (TextView) this .findViewById(R.id. text_view ); 
中间加横线 
tv.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); 
底部加横线: 
tv .getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );

19.取消GridView单击时的黄色背景:

mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));

 

20.Android修改背景图片:

ImageView iv = new ImageView(this); iv.setBackgroundResource(R.id.bg);

21.Toast:

Toast.makeText(this, "提醒发货成功!", Toast.LENGTH_LONG).show();

22.刷新当前Activity:

Intent intent = new Intent(currentActivity.this, currentActivity.class);
startActivity(intent);
finish();//结束当前Activiy

23.Android隐藏滚动条:

android:scrollbars="none"

24.Fragment调用Activity中的方法:

((YourActivityClassName)getActivity()).yourPublicMethod();

25.在Adapter中关闭当前Activity:

((OrderCenterActivity)context).finish();

26.Android复制粘贴功能:

API 11之前: android.text.ClipboardManager
API 11之后: android.content.ClipboardManager/**
  * 实现文本复制功能 
  * 
  * @param content
  */
 public static void copy(String content, Context context) {
  // 得到剪贴板管理器
  ClipboardManager cmb = (ClipboardManager) context
    .getSystemService(Context.CLIPBOARD_SERVICE);
  cmb.setText(content.trim());
 } /**
  * 实现粘贴功能 
  * 
  * @param context
  * @return
  */
 public static String paste(Context context) {
  // 得到剪贴板管理器
  ClipboardManager cmb = (ClipboardManager) context
    .getSystemService(Context.CLIPBOARD_SERVICE);
  return cmb.getText().toString().trim();
 }

 

27.解决Andriod软键盘出现把原来的布局给顶上去的方法

<activity 
android:name=".activity.HomeActivity" 
android:windowSoftInputMode="adjustPan|stateHidden"

28.设置EditText是否可编辑:

设置不可编辑状态
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);设置可编辑状态
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
editText.requestFocus();

29.带图片的toast:

Android常用控件技巧总结_android

//带图片的toast
 public static void showToast(Context context, String mes, int resId) {
  Toast toast = Toast.makeText(context, mes, Toast.LENGTH_SHORT);
  toast.setGravity(Gravity.CENTER, 0, 0);
  LinearLayout toastView = (LinearLayout) toast.getView();
  ImageView imageCodeProject = new ImageView(context);
  imageCodeProject.setImageResource(resId);
  toastView.addView(imageCodeProject, 0);
  toast.show();
 }

 

30.Android回到桌面

Intent i = new Intent(Intent.ACTION_MAIN);
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  i.addCategory(Intent.CATEGORY_HOME);
  startActivity(i);

31.单击dialog之外的地方,可以dismiss掉dialog。
 

setCanceledOnTouchOutside(true);


  
32.Android暂停几秒

Timer timer = new Timer();  
         TimerTask tast = new TimerTask() {  
             @Override  
             public void run() {  
                 startActivity(localIntent);  
             }  
         };  
         timer.schedule(tast, 1500);

33.Android实现将drawable转成bitmap:

public static Bitmap drawableToBitmap(Drawable drawable) {      
  BitmapDrawable bd = (BitmapDrawable) drawable;
  Bitmap bm = bd.getBitmap();
  return bm;
 }

34.popwindow点击外部关闭:

popView = inflater.inflate(R.layout.sort_dialog, null);              // 读取布局管理器  
popWin = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,  
                 true);   popWin.setFocusable(true);
 popWin.setBackgroundDrawable(new PaintDrawable());
 popWin.setOutsideTouchable(true);

35.设置popwindow的显示位置:

popWin.showAsDropDown(findView(R.id.btn_sort),10,10);
  //popWin.showAtLocation(findView(R.id.btn_sort), Gravity.TOP|Gravity.RIGHT, 10, 100);

 

36.TextView单击短暂改变背景色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">            
    <item android:state_pressed="true" android:drawable="@color/yellow" />    
    <item  android:drawable="@color/white" />   
</selector>

37.让scrollView滚动到顶部:
在scrollView内部最上面定义一个控件获得焦点,滚动条自然就到顶部去了,如下:

txtBaseMsg.setFocusable(true);
txtBaseMsg.setFocusableInTouchMode(true);
txtBaseMsg.requestFocus();

38.三星手机拍照报错的问题解决:

在activity中添加:android:configChanges="keyboardHidden|orientation|screenSize"

 

39.获取手机拍摄照片的时间:

String img_date = Utils.DATE_TIME_FORMATER.format(new Date());
      try {
       ExifInterface exif = new ExifInterface(mDirPath);
       String str = exif.getAttribute(ExifInterface.TAG_DATETIME);
       String temp[] = str.split(" ");
       temp[0] = temp[0].replace(":","-");
       img_date = temp[0] + " " + temp[1];
       LogUtil.e("img_date:"+img_date);
      } catch (Exception ee) {
       ee.printStackTrace();
      }

 

40.在listView 中嵌套了 Gridview,然后点击item事件没有反应 

首先在listview的item的XML文件的最外层加入
android:descendantFocusability="blocksDscendants"

然后再adapter的java文件中获取gridview

设置

holder.imgGrid.setClickable(false);
holder.imgGrid.setPressed(false);
holder.imgGrid.setEnables(false);

举报

相关推荐

0 条评论