我正在尝试创建自定义ViewGroup,我想将它与全屏应用程序一起使用。我正在使用“requestWindowFeature(Window.FEATURE_NO_TITLE)”来隐藏标题栏。标题栏未显示,但仍占用窗口顶部的空间。
上面的图片是使用以下代码生成的:
public class CustomLayoutTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Button b = new Button(this);
b.setText("Hello");
CustomLayout layout = new CustomLayout(this);
layout.addView(b);
setContentView(layout);
}
}
public class CustomLayout extends ViewGroup {
public CustomLayout(Context context) {
super(context);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.i("CustomLayout", "changed="+changed+" l="+l+" t="+t+" r="+r+" b="+b);
final int childCount = getChildCount();
for (int i = 0; i < childCount; ++i) {
final View v = getChildAt(i);
v.layout(l, t, r, b);
}
}
}
(The full Eclipse project is here)
有趣的是,我的自定义布局为Android提供了空间。我将CustomLayout设置为我的Activity的根布局。在登录中,“onLayout”正在接收“t = 25”,这就是推动我的布局。我不知道的是我做错了什么让Android成为“t = 25”(这正是标题栏的高度)。
我在Android SDK 2.1中运行此代码,但我也在Android 2.2中运行。
编辑:如果我更改某些默认布局的CustomLayout类(例如LinearLayout),则该空格将消失。当然,Android SDK的默认布局不会创建我想要创建的布局,所以这就是我创建一个布局的原因。
虽然我正在创建的布局有点复杂,但这是我创建的最小代码,可以重现我的布局问题。
答案 0 :(得分:3)
这不是一个完整的答案,但与此同时,您可以通过将自定义布局包装在<FrameLayout />
此外,值得注意的是,您的布局超出了屏幕底部。它向下移动标题栏高度(我的模拟器中为38像素)
编辑:知道了。 onLayout()(和相应的layout()方法)指定坐标不是相对于屏幕原点,它们是相对于父(http://developer.android.com/reference/android/view/View.html#layout%28int,%20int,%20int,%20int%29)。所以系统告诉你你在相对坐标(0,38),并且在将它传递给你的孩子时你正在添加它,这意味着你说你的孩子在屏幕坐标(0, 76),造成差距。
你真正想做的是:
v.layout(0, 0, r - l, b - t);
这会使您的子视图与视图的左上角对齐,其宽度和高度与视图相同。
答案 1 :(得分:0)
我在2.2中的FrameLayout
遇到了同样的问题
我通过将android:layout_gravity="top"
添加到FrameLayout