安卓面试

Android 日常问题整理 (一)

2019-12-18  本文已影响0人  飞指

适配红米Note3 Toast 显示出现 应用程序名称:


    public void showToast(Context context, String msg) {
        Toast toast = new Toast(context);
        toast.setText(msg);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

Button 去掉背景阴影

style="?android:attr/borderlessButtonStyle"

判断当前Activity 是否处于应用程序的顶端

private boolean isTopActivity() {
        boolean isTop = false;
        ActivityManager am = (ActivityManager) App.getContext().getSystemService(ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        if (cn.getClassName().contains(TechOrderDetailAct.class.getName())) {
            isTop = true;
        }
        return isTop;
    }

Android实现底部弹窗重要样式

  <!--设置style 样式-->
   <style name="custom_dialog2_" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!-- Dialog的windowFrame框为无 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否漂现在activity上 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 是否半透明 -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@null</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowContentOverlay">@null</item>
        <!-- 去除黑色边框的关键设置项 -->
        <item name="android:backgroundDimEnabled">true</item>
    </style>

<?xml version="1.0" encoding="utf-8"?>
<!--设置背景样式-->
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp" />
    <solid android:color="@color/white" />

</shape>
//Java 实现代码
View contentView = getLayoutInflater().inflate(R.layout.contact_to_merchant, null);
        final Dialog dialog = new Dialog(this, R.style.custom_dialog2_);
        dialog.setContentView(contentView);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.drawable.dialog_top_radios);
        dialog.show();
        
        //布局样式代码
<?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="wrap_content"
    android:orientation="vertical"
    android:padding="20dp">


    <Button
        android:id="@+id/call_phone"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/ok_to_pay"
        android:text="呼叫 13244669955"
        android:textColor="@color/white"
        android:textSize="16sp" />

    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/ok_to_pay"
        android:text="取消"
        android:textColor="@color/white"
        android:textSize="16sp" />

</LinearLayout>

判断当前应用程序处于前台还是后台

   public static boolean isApplicationBroughtToBackground(final Context context) {
       ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
       List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
       if (!tasks.isEmpty()) {
           ComponentName topActivity = tasks.get(0).topActivity;
           if (!topActivity.getPackageName().equals(context.getPackageName())) {
               return true;
           }
       }
       return false;
   }

Glide 加载动画

//getApplicationContex() 可避免出现Glide在加载过程中报错
//asGif()  指定加载的资源为Gif 动态图
//DiskCacheStrategy.SOURCE 为了加载更快一点二
Glide.with(getApplicationContext()).load(R.drawable.reward).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(reward);

Idea 打包Android项目出现cannot recover key 的解决方法:

重新输入密码即可

Android 使用广播时出现:Caused by: java.lang.InstantiationException: java.lang.Class<com.hunan.juyan.module.self.act.TechOrderDetailAct$OrderBroadcastReceiver> has no zero argument constructor 问题的处理方式

原因是内部广播接收者没有构造函数,可以将广播接收者设置为静态的即可

如何将Uri 转换成真实文件路径

    /**
    * 将Uri 转换成真实路径
    * @param contentUri
    * @return
    */
   public String getRealPathFromURI(Uri contentUri) {
       String res = null;
       String[] proj = {MediaStore.Images.Media.DATA};
       Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
       if (cursor.moveToFirst()) {
           ;
           int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
           res = cursor.getString(column_index);
       }
       cursor.close();
       return res;
   }
上一篇 下一篇

猜你喜欢

热点阅读