我使用的是来自here的QuickAction代码,在弄了一周之后,我遇到了一些困难,因为它在某些尺寸的屏幕上不能正确显示。我已经将问题缩小到代码中的一行,但似乎无法同时在我的手机和模拟器上正确显示它。请允许我解释一下。
有问题的行在"PopupWindows.java“文件中,如下所示:
public static final int MAX_WIDTH_POPUP_WINDOWS = 435;我已经将其更改为"433“,因为它与2.3,4.0.1和4.1模拟器上的3个图标非常适合,分辨率为800x480,如下所示:

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

奇怪的是,当我将该数字变为865时,它在手机上正确地呈现出来

但在仿真器中显示如下所示:

我曾尝试更改用于快速操作的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>为这篇长篇文章提前道歉,但这是我的一个习惯,因为我在支持部门工作,并相信提前提供最详细的信息有助于更快地解决问题:)
谢谢!
发布于 2013-05-30 13:16:32
你不应该使用硬编码的像素值。解决此问题的最简单方法是在xml资源文件中声明一个dimension。然后在你的代码中,你可以这样做:
width = getResources().getDimensionPixelSize(R.dimen.popup_width);这将得到根据密度调整的像素数。您应该计算出每个按钮有多少个DPs,并相应地声明资源。
https://stackoverflow.com/questions/16828210
复制相似问题