开发者

How to remove the black space between the title bar and the first element of a LinearLayout?

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:

How to remove the black space between the title bar and the first element of a LinearLayout?


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);
}


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


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜