如何删除标题栏和 LinearLayout 的第一个元素之间的黑色空间?

发布于 2024-12-09 03:50:40 字数 1323 浏览 0 评论 0原文

我有一个线性布局,其中第一个元素是图像视图标题,第二个元素是网格视图。

它工作正常,但我在 android 标题栏和应用程序标题之间有一个错误的 50px(或多或少)的黑色空间,这是线性布局的第一个元素

为什么我有这个空间?我发现删除它的唯一方法是放置此行: ll.setPadding(0, -50, 0, 0);

这是完整的代码:

public class MainGrid extends Activity {
    private GridView myGridView;
    private ImageAdapter myImageAdapter;
    private ImageView header;
    
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);//turn off the window's title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//fullscreen
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setGravity(Gravity.CENTER);
    //ll.setPadding(0, -50, 0, 0);
    header = new ImageView(getApplicationContext());
    header.setImageResource(R.drawable.header_acc);
    
    myGridView = new GridView(this);
    myImageAdapter=new ImageAdapter(this);
    myGridView.setAdapter(myImageAdapter);

    ll.addView(header);
    ll.addView(myGridView);
    setContentView(ll);
}

快照:

在此处输入图像描述

I have a linearlayout, which the first element is a imageview header, and the second element is a gridview.

It works fine, but I have a erroneal black space of 50px (more or less) between the android title bar and the header of the app, which is the first element of the linearlayout

Why do I have that space? The only way I find to remove it is to put this line: ll.setPadding(0, -50, 0, 0);

This is the full code:

public class MainGrid extends Activity {
    private GridView myGridView;
    private ImageAdapter myImageAdapter;
    private ImageView header;
    
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);//turn off the window's title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//fullscreen
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setGravity(Gravity.CENTER);
    //ll.setPadding(0, -50, 0, 0);
    header = new ImageView(getApplicationContext());
    header.setImageResource(R.drawable.header_acc);
    
    myGridView = new GridView(this);
    myImageAdapter=new ImageAdapter(this);
    myGridView.setAdapter(myImageAdapter);

    ll.addView(header);
    ll.addView(myGridView);
    setContentView(ll);
}

The snapshot:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海螺姑娘 2024-12-16 03:50:40

更新:如果您在清单中设置 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" ,这应该可以正常工作。

public class MainGrid extends Activity {
    private GridView myGridView;
    private ImageAdapter myImageAdapter;
    private ImageView header;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setGravity(Gravity.TOP);   // SET THIS TO TOP

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
    ll.setLayoutParams(lp);
    // I'VE ADDED LAYOUTPARAMS TOO

    header = new ImageView(getApplicationContext());
    header.setImageResource(R.drawable.header_acc);

    myGridView = new GridView(this);
    myImageAdapter=new ImageAdapter(this);
    myGridView.setAdapter(myImageAdapter);

    ll.addView(header);
    ll.addView(myGridView);
    setContentView(ll);
}

Update: This should work just fine if you set android:theme="@android:style/Theme.NoTitleBar.Fullscreen" in the Manifest.

public class MainGrid extends Activity {
    private GridView myGridView;
    private ImageAdapter myImageAdapter;
    private ImageView header;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setGravity(Gravity.TOP);   // SET THIS TO TOP

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
    ll.setLayoutParams(lp);
    // I'VE ADDED LAYOUTPARAMS TOO

    header = new ImageView(getApplicationContext());
    header.setImageResource(R.drawable.header_acc);

    myGridView = new GridView(this);
    myImageAdapter=new ImageAdapter(this);
    myGridView.setAdapter(myImageAdapter);

    ll.addView(header);
    ll.addView(myGridView);
    setContentView(ll);
}
枫林﹌晚霞¤ 2024-12-16 03:50:40

如果您不使用标题栏,则可以随时将其删除:

If you're not using the title bar, you can always get rid of it:

http://elliotth.blogspot.com/2009/07/removing-title-bar-from-your-android.html

呆° 2024-12-16 03:50:40

在清单中使用它的活动标签可以解决您的问题

android:theme="@android:style/Theme.NoTitleBar"

对于全屏,您应该使用

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

Using this in for activity tag in manifest solves your problem

android:theme="@android:style/Theme.NoTitleBar"

For Full screen you should use

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文