我有一个问题,我要向一个活动添加三个 ImageView,一个是页眉,另一个是页脚,第三个位于 ScrollView 之间。我正在使用版本 android_2.1_r1 并且我注意到一个非常奇怪的问题,无法弄清楚发生了什么,尽管我可以告诉它为什么会发生。
似乎如果我得到 DecorView 并要求可见矩形,它是 x=0 y=38 作为屏幕的顶部?为什么这样做。如果我将顶部图像的大小更改为大于 40,它会消失吗?
以防万一有人想到它,而不是下面。我有这一套。
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
请参阅随附的屏幕截图。红色条应与顶部齐平。但出于某种原因,DecorView 决定顶部有一个插图??????任何帮助表示赞赏。
更新 1
所以我发现了问题,它是ScrollView.setFocusableInTouchMode,请参阅下面的代码。当注释掉的行未注释时,它会导致问题。但前提是顶部图像视图的高度小于 20。有人能解释一下吗??????我努力了。
- 相对布局
- 绝对布局
- 线性布局
更新 2
更奇怪的是,当我旋转,然后旋转回来,它固定!x=0, y=38 插图不见了????我屏幕顶部的黑线消失了???
更新 3
当我显示软键盘时,这个问题就消失了,就好像它重置了什么一样。我还可以通过使用反射将根视图插入设置为 0 来使用 OnGlobalLayoutListener 修复它。
public void onGlobalLayout() {
AndroidActivity activity = screen.getActivity();
Window window = activity.getWindow();
View view = window.getDecorView();
view.getWindowVisibleDisplayFrame(currentlyVisibleRect);
if(currentlyVisibleRect.top > 0) {
// Below code will not work....but you get the idea
Field field1 = view.getClass().getDeclaredField("mAttachInfo");
Field field2 = field.get(view).getClass().getDeclaredField("mVisibleInsets");
Rect rect = field2.get(field1.get(view));
rect.top = 0;
}
}
我宁愿没有人建议 XML 布局,我的应用程序必须是非常动态的,并且响应关于如何创建布局的各种规则。
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.ScrollView;
public class AndroidLayoutManager {
private final Activity activity;
private final Rectangle bounds;
private final String name;
public AndroidLayoutManager(Activity activity, Rectangle bounds, String name) {
this.activity = activity;
this.bounds = bounds;
this.name = name;
}
public ViewGroup createLayout(ViewLayout viewLayout, ViewComponent headerView, ViewComponent mainView, ViewComponent footerView) {
AbsoluteLayout relativeLayout = new AbsoluteLayout(activity);
AbsoluteLayout.LayoutParams fullScreenParams = new AbsoluteLayout.LayoutParams(bounds.width, bounds.height, 0, 0);
if(viewLayout.isHeaderIncluded()) {
AbsoluteLayout.LayoutParams headerViewParams = headerView.getLayoutParams();
View view = headerView.getView();
view.setLayoutParams(headerViewParams);
relativeLayout.addView(view, headerViewParams);
}
if(viewLayout.isMainIncluded()) {
AbsoluteLayout.LayoutParams mainViewParams = mainView.getLayoutParams();
View view = mainView.getView();
View scrollView = createScrollView(view);
scrollView.setLayoutParams(mainViewParams);
relativeLayout.addView(scrollView, mainViewParams);
}
if(viewLayout.isFooterIncluded()) {
AbsoluteLayout.LayoutParams footerViewParams = footerView.getLayoutParams();
View view = footerView.getView();
view.setLayoutParams(footerViewParams);
relativeLayout.addView(view, footerViewParams);
}
relativeLayout.requestLayout();
activity.setContentView(relativeLayout, fullScreenParams);
mainView.getView().requestFocus();
activity.getWindow().getDecorView().requestLayout();
return relativeLayout;
}
private View createScrollView(View view) {
ScrollView scrollView = new ScrollView(activity);
// ******** WTF??? This is causing a strange layout problem *******
//scrollView.setFocusableInTouchMode(true);
scrollView.requestFocus();
scrollView.setFocusable(true);
scrollView.setVerticalScrollBarEnabled(false);
scrollView.addView(view);
scrollView.setFillViewport(true);
return scrollView;
}
@Override
public String toString() {
return name;
}
public static class ViewLayout {
private Rectangle header;
private Rectangle main;
private Rectangle footer;
public ViewLayout(Rectangle header, Rectangle main, Rectangle footer) {
this.header = header;
this.main = main;
this.footer = footer;
}
public boolean isMainIncluded() {
return main != null;
}
public boolean isFooterIncluded() {
return footer != null;
}
public boolean isHeaderIncluded() {
return header != null;
}
public Point getHeaderPoint() {
return new Point(0, 0);
}
public Point getMainPoint() {
if(header != null) {
return new Point(0, header.height);
}
return new Point(0, 0);
}
public Point getFooterPoint() {
if(header == null) {
return new Point(0, main.height);
}
return new Point(0, main.height + header.height);
}
public int getHeight() {
int totalHeight = main.height;
if(header != null) {
totalHeight += header.height;
}
if(footer != null) {
totalHeight += footer.height;
}
return totalHeight;
}
}
public static class ViewComponent {
private final Rectangle rectangle;
private final Point origin;
private final View view;
public ViewComponent(View view, Rectangle rectangle, Point origin) {
this.view = view;
this.rectangle = rectangle;
this.origin = origin;
}
public AbsoluteLayout.LayoutParams getLayoutParams() {
return new AbsoluteLayout.LayoutParams(rectangle.width, rectangle.height, origin.x, origin.y);
}
public View getView() {
return view;
}
}
}