我正在使用此处的 QuickAction 代码,并且在弄乱了一周后无法在某些屏幕尺寸上正确显示后遇到了一些困难。我已将问题缩小到代码中的一行,但似乎无法同时在我的手机和模拟器上正确显示。请允许我解释一下。
有问题的行位于“PopupWindows.java”文件中,如下所示:
public static final int MAX_WIDTH_POPUP_WINDOWS = 435;
我已将其更改为“433”,因为它与分辨率为 800x480 的 2.3、4.0.1 和 4.1 模拟器上的 3 个图标完美匹配,如下所示:
但在手机(1920x1080)上,它呈现如下:
奇怪的地方是当我把那个号码变成 865 时,它会在手机上正确呈现
但随后在模拟器中显示如下:
我尝试更改用于快速操作的 xml 以包装内容,以及设置硬编码宽度,但这并不能解决问题。请参见下面的代码:
弹出窗口.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>
提前很抱歉这篇长文,但这是我在支持工作时的一种习惯,并且相信预先提供最详细的信息有助于更快地解决问题:)
谢谢!