安卓学习

本文介绍如何使用XML自定义Android中的按钮和输入框样式,实现圆角效果及内部填充;并展示了去除TabHost分割线的方法,RecyclerView的分割线与间距设置技巧,以及CardView的使用方式。此外,还提供了不同格式间数据转换的代码示例,如Bitmap与byte数组、Drawable之间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~圆角按钮 xml文件放在drawable里 设置background


<?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> 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~输入框 同上


<?xml version="1.0" encoding="utf-8"?>
     <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle" >


     <!--     <gradient
          android:angle="45"
          android:endColor="#CCCCCC"
         android:startColor="#CCCCCC" /> -->


         <padding
             android:bottom="7dp"
             android:left="7dp"
             android:right="7dp"
             android:top="7dp" />
        <!-- 设置圆角矩形 -->
         <corners android:radius="3dp" />


         <stroke
             android:width="1dp"
             android:color="#CCCCCC" />


         <solid android:color="#FFFFFF" />


     </shape>




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~去掉tabhost之间的分割线


android:showDividers="none"




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~recyclerView 分割线


recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
                DividerItemDecoration.VERTICAL_LIST));


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~recyclerView 间距
int spacingInPixels = 8;
recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~如果确定每个item的内容不会改变RecyclerView的大小,设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true); 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cardView
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="10dp"  
    app:cardElevation="2dp"     
    app:contentPaddingBottom="5dip">  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~图片转换


1.  Bitmap 转化为 byte
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
byte[] array= out.toByteArray();
同上
 public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }




2. byte转化为bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);


3. bitmap转化为Drawable
Drawable drawable = new FastBitmapDrawable(bitmap);


4. Drawable转化为bitmap
 a. BitmapDrawable, FastBitmapDrawable直接用getBitmap
 b. 其他类型的Drawable用Canvas画到一个bitmap上
      Canvas canvas = new Canvas(bitmap)
      drawable.draw(canvas)




5.id转化graphic.drawable


   Drawable drawable = getResources().getDrawable(R.drawable.icon);




6.id转化成Bitmap


   Bitmap bitmap = BitmapFactory. decodeResource (Resources   res, int id)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~输入一个随机数
editor.putInt("random", (int) (Math.random() * 100));


1。MainActivity中包括一个ImageView;当我们点击ImageView时;把图片传递给另外一个Activity
Intent intent=new Intent(MainActivity.this,TranActivity.class);
            intent.putExtra("bitmap", bitmap);
            startActivity(intent);
2。在TranActivity中接收MainActivity传递过来的Bitmap;
imageview=(ImageView)findViewById(R.id.trans_imageview);
        Intent intent=getIntent();
        if(intent!=null)
        {
            bitmap=intent.getParcelableExtra("bitmap");
            imageview.setImageBitmap(bitmap);
        }
3。通过接受图片之后,可以放大图片
imageview = (ImageView) findViewById(R.id.trans_imageview);
        Intent intent = getIntent();
        if (intent != null) {
            bitmap = intent.getParcelableExtra("bitmap");
            Matrix matrix = new Matrix(); //接收图片之后放大 1.5倍
            matrix.postScale(1.5f, 1.5f);
            Bitmap bit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);
            imageview.setImageBitmap(bit);
        }


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
view.bringToFront() 可以讲布局在下层的控件放到上层,不被其他控件挡住。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~锁屏显示activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);


我们可以通过如下方法判断,系统是否处在锁屏状态:
KeyguardManager km =  
    (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean showingLocked = km.inKeyguardRestrictedInputMode(); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值