0

我使用以下代码添加了自定义标题栏。但是,我似乎在底部左右有大约 10 px 间距和 2px,有什么办法可以使自定义标题栏适合标题栏区域?

customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.tab);
        Display display = getWindowManager().getDefaultDisplay(); 
        
        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                            R.layout.titlebar);
            
            LinearLayout titlebar = (LinearLayout) findViewById(R.id.TitleBarLayOut);
            titlebar.setPadding(-2, -1, -2, -1);
            titlebar.setMinimumWidth(display.getWidth());
            GradientDrawable grad = new GradientDrawable(Orientation.TOP_BOTTOM,
                      new int[] {Color.RED, Color.YELLOW}
                    );
            titlebar.setBackgroundDrawable(grad);

            playButton = new Button(this);
            playButton.setText("Play");
            playButton.setGravity(Gravity.LEFT);
            
            infoButton = new Button( this );
            infoButton.setText("Info");
            infoButton.setGravity(Gravity.RIGHT);
            
            titlebar.addView(playButton, new LinearLayout.LayoutParams(
                      70,1));
            
            titlebar.addView(infoButton, new LinearLayout.LayoutParams(
                  70,1));
            
            
        }


<?xml version="1.0" encoding="utf-8"?>

4

3 回答 3

2

您想填充实际标题栏的背景 - 而不是您的自定义区域。试试这里对我有用的解决方案:设置标题背景颜色

于 2011-08-14T16:27:20.920 回答
0

您想使用FILL_PARENT作为宽度。在 XML 中:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
于 2011-02-25T05:32:35.213 回答
0

今天,这很简单,因为我在 2011 年知道它并不那么简单。设置为“Top”的属性“Dock”将为您填充它。

titlebar.Dock = DockStyle.Top;
于 2019-08-09T21:03:31.453 回答