小编rpe*_*yng的帖子

Android Canvas清晰透明

我试图从画布上"擦除".就像清理画布的非常简单的测试一样,我实现了以下内容:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.argb(140, 0, 0, 0));
    canvas.drawColor(0, Mode.CLEAR);
}
Run Code Online (Sandbox Code Playgroud)

Color.argb(140, 0, 0, 0)设置此视图被绘制的视图以使其变暗. drawColor(0, Mode.CLEAR)使屏幕完全变黑,而不是删除之前应用的调光. 这个想法来自这里

android android-custom-view android-canvas

16
推荐指数
2
解决办法
2万
查看次数

Android点击PopupWindow

我创建了一个扩展popupwindow的类.它的构造函数看起来如下所示

super(builder.context.get());

this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);
FrameLayout frameLayout = new FrameLayout(builder.context.get());
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false);
frameLayout.addView(builder.backgroundView);
frameLayout.addView(overlayBase);
setContentView(frameLayout);
Run Code Online (Sandbox Code Playgroud)

onTouchListener如下所示:

private OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Log.d(TAG, "Received");

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) {
            Log.d(TAG, "bubbled through");
            return false;
        }
        return true;
    }
};
Run Code Online (Sandbox Code Playgroud)

我打电话来实际显示弹出窗口 SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), …

android z-order onclicklistener

10
推荐指数
1
解决办法
5028
查看次数

Android ScrollView不尊重孩子的底部保证金

我有一个android layout_file,看起来像下面这样

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/post_ride_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_margin"
        android:padding="@dimen/activity_margin"
        android:background="@drawable/google_now_style_card"
        android:orientation="vertical" >

    ...

    </LinearLayout
</ScrollView
Run Code Online (Sandbox Code Playgroud)

@dimen/activity_margin 是16dp

当我运行应用程序时,我无法滚动到整个布局的底部,因为它不会显示底部边距.这张照片澄清了

在此输入图像描述

请注意,已达到布局底部的提示(从底部发出的光线),但右侧的滚动条表示还有更多内容可向下滚动.

我期望发生的是能够看到底部的边距,就像布局侧面的边距一样.

android android-layout android-scrollview

9
推荐指数
2
解决办法
5669
查看次数

当对话框显示时,Android会向主要活动发送触摸事件

我有一个DialogFragment,它onCreateDialog()看起来像

Dialog dialog = new Dialog(getActivity(), R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(parentView);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));

parentView.setOnTouchListener(onTouchListener);
return dialog;
Run Code Online (Sandbox Code Playgroud)

onTouchListener看起来像

private OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Bundle bundle = getArguments();
        int[] coordinates = bundle.getIntArray(COORDINATES);
        int radius = bundle.getInt(RADIUS);

        float x = event.getX();
        float y = event.getY();

        Log.d(TAG, "called touch");

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            getActivity().dispatchTouchEvent(event);
            Log.d(TAG, "bubbled");
        }
        return false; …
Run Code Online (Sandbox Code Playgroud)

android ontouchlistener android-activity android-dialogfragment

8
推荐指数
0
解决办法
2517
查看次数

Android Dim自定义对话框背景

标题是,我似乎无法调暗我所做的自定义对话框的背景.无数解决方案在线提到了下面第一个代码段中的最后3行代码,这对对话框的UI没有任何影响.

请参阅以下代码:

Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
TextView textView = (TextView) dialog.findViewById(R.id.textView);
textView.setText("Custom Text Example");
dialog.show();

WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.dimAmount = .7f;
dialog.getWindow().setAttributes(layoutParams);
Run Code Online (Sandbox Code Playgroud)

自定义对话框的布局xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progressDialogCustom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialog_black"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:text="Updating Profile . . ." />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

@drawable/dialog_black文件如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@android:color/background_dark" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

    <stroke
        android:width="1px"
        android:color="@android:color/darker_gray" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" /> …
Run Code Online (Sandbox Code Playgroud)

android android-ui android-xml android-dialog

6
推荐指数
1
解决办法
7857
查看次数

gson 将特定字段解释为大写

我有一个包含一些字段的类,例如

public class Message {

    public Enum Type {
        TEXT,
        PICTURE;
    }

    private Type type;
    private String text;
    private int id;
}
Run Code Online (Sandbox Code Playgroud)

假设我有一个如下所示的 json

[ { "id" : "1",
    "data" : "hello",
    "type" : "text",
  },
  { "id" : "2",
    "data" : "<someImageUrl>",
    "type" : "picture",
  },
]
Run Code Online (Sandbox Code Playgroud)

我可以通过调用轻松反序列化它

Gson gson = new Gson();
ArrayList<Message> messages = gson.fromJson(jsonResult, new TypeToken<List<Message>>() {}.getType());
Run Code Online (Sandbox Code Playgroud)

凉爽的。不幸的是,由于 json 对象和枚举中定义的实例之间的大小写差异,Type 字段仍然为空。如何更改 gson 的行为,以便它将 json Type 对象解释为大写。

我想到的潜在解决方法:

  • 将序号值分配给将匹配 json 中给出的内容的枚举类型(即 TEXT("text")
  • 用分隔符“}”分割json结果,
  • 用 -SOMEE​​NUMTYPE- 替换所有出现的 -someEnumType- …

java enums serialization json gson

5
推荐指数
2
解决办法
1893
查看次数