在QuickAction无法正确显示的情况下苦苦挣扎

时间:2013-05-30 04:56:06

标签: java android quickaction

我正在使用来自here的QuickAction代码,并且在使用它一周后在某些屏幕尺寸上无法正确显示时遇到了一些困难。我已经将问题缩小到代码中的一行,但似乎无法在我的手机和模拟器上同时显示它。请允许我解释一下。

有问题的行位于“PopupWindows.java”文件中,如下所示:

 public static final int MAX_WIDTH_POPUP_WINDOWS = 435;

我已将其更改为“433”,因为它与2.3,4.0.1和4.1仿真器上的3个图标完美匹配,分辨率为800x480,如下所示:

enter image description here

但在电话(1920x1080)上,它呈现如下:

enter image description here

它变得奇怪的是,当我把这个数字转为865时,它会在手机上正确呈现

enter image description here

然后在模拟器中显示如下:

enter image description here

我已经尝试更改xml以进行快速操作以包装内容,以及设置硬编码宽度,但这并不能解决问题。请参阅以下代码:

PopupWindows.java

package actionitem;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater; 
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * Custom popup window.
 * 
 * @author Lorensius W. L. T <lorenz@londatiga.net>
 * 
 */
public class PopupWindows {
protected Context mContext;
protected PopupWindow mWindow;
protected View mRootView;
protected Drawable mBackground = null;
protected WindowManager mWindowManager;
Resources res;
// public static final int MAX_WIDTH_POPUP_WINDOWS = 433;

public static final int MAX_WIDTH_POPUP_WINDOWS = 865;

/**
 * Constructor.
 * 
 * @param context
 *            Context
 */
public PopupWindows(Context context) {
    mContext = context;
    mWindow = new PopupWindow(context);

    mWindow.setTouchInterceptor(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                mWindow.dismiss();

                return true;
            }

            return false;
        }
    });

    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}

/**
 * On dismiss
 */
protected void onDismiss() {
}

/**
 * On show
 */
protected void onShow() {
}

/**
 * On pre show
 */
protected void preShow() {
    if (mRootView == null)
        throw new IllegalStateException("setContentView was not called with a view to display.");

    onShow();

    if (mBackground == null)
        mWindow.setBackgroundDrawable(new BitmapDrawable(res));
    else
        mWindow.setBackgroundDrawable(mBackground);

    int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
    int w = screenWidth < MAX_WIDTH_POPUP_WINDOWS ? screenWidth : MAX_WIDTH_POPUP_WINDOWS;
    // if (screenWidth < 490) {
    // w = 433;
    // } else if (screenWidth > 865) {
    // w = 865;
    // }
    mWindow.setWidth(w);// WindowManager.LayoutParams.WRAP_CONTENT);
    mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    mWindow.setTouchable(true);
    mWindow.setFocusable(true);
    mWindow.setOutsideTouchable(true);

    mWindow.setContentView(mRootView);
}

/**
 * Set background drawable.
 * 
 * @param background
 *            Background drawable
 */
public void setBackgroundDrawable(Drawable background) {
    mBackground = background;
}

/**
 * Set content view.
 * 
 * @param root
 *            Root view
 */
public void setContentView(View root) {
    mRootView = root;

    mWindow.setContentView(root);
}

/**
 * Set content view.
 * 
 * @param layoutResID
 *            Resource id
 */
public void setContentView(int layoutResID) {
    LayoutInflater inflator = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    setContentView(inflator.inflate(layoutResID, null));
}

/**
 * Set listener on window dismissed.
 * 
 * @param listener
 */
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
    mWindow.setOnDismissListener(listener);
}

/**
 * Dismiss the popup window.
 */
public void dismiss() {
    mWindow.dismiss();
}
}

和我的quickaction.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="220dp"
android:layout_height="wrap_content" >

<FrameLayout
    android:id="@+id/header2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/scroll"
    android:layout_marginTop="10dip"
    android:background="@drawable/quickaction_top_frame" />

<ImageView
    android:id="@+id/arrow_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/quickaction_arrow_up" />

<HorizontalScrollView
    android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/header2"
    android:background="@drawable/quickaction_slider_background"
    android:fadingEdgeLength="0dip"
    android:paddingLeft="1dip"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/tracks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="4dip"
        android:paddingTop="4dip" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quickaction_slider_grip_left" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quickaction_slider_grip_right" />
    </LinearLayout>
</HorizontalScrollView>

<FrameLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignRight="@id/scroll"
    android:layout_below="@id/scroll"
    android:background="@drawable/quickaction_bottom_frame" />

<ImageView
    android:id="@+id/arrow_down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/footer"
    android:layout_marginTop="-1dip"
    android:src="@drawable/quickaction_arrow_down" />

</RelativeLayout>

对于这篇长篇文章提前抱歉,但这是我习惯支持的习惯,并且相信提前提供最详细的信息有助于尽快解决问题:)

谢谢!

1 个答案:

答案 0 :(得分:0)

您不应使用硬编码像素值。解决此问题的最简单方法是在xml资源文件中声明dimension。然后在您的代码中,您可以这样做:

  width = getResources().getDimensionPixelSize(R.dimen.popup_width);

这将为您提供根据密度调整的像素数。您应该弄清楚每个按钮的DP数量,并相应地声明资源。