我正在尝试实现自定义标题栏:
这是我的助手课程:
import android.app.Activity;
import android.view.Window;
public class UIHelper {
public static void setupTitleBar(Activity c) {
final boolean customTitleSupported = c.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
c.setContentView(R.layout.main);
if (customTitleSupported) {
c.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在onCreate()中调用它的地方:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupUI();
}
private void setupUI(){
setContentView(R.layout.main);
UIHelper.setupTitleBar(this);
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
requestFeature() must be called before adding content
Run Code Online (Sandbox Code Playgroud) 之前已经问过这个问题:AlertDialog自定义标题有黑色边框
但没有得到满意的答复 - 并且缺少一些信息.
我正在尝试在没有标题的情况下在Android中创建自定义对话框,并且底部没有任何按钮.
但是,生成的对话框在视图的顶部和底部有黑色"边框"/"间距"/某些内容.
从文档:
使用基本Dialog类创建的对话框必须具有标题.如果不调用setTitle(),则用于标题的空间仍为空,但仍然可见.如果您根本不需要标题,则应使用AlertDialog类创建自定义对话框.但是,因为使用AlertDialog.Builder类创建的AlertDialog最简单,所以您无权访问上面使用的setContentView(int)方法.相反,您必须使用setView(View).此方法接受View对象,因此您需要从XML中扩展布局的根View对象.
那就是我做的:
Welcome.java
public class Welcome extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.welcomedialog, (ViewGroup)findViewById(R.id.layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.create().show();
}
}
Run Code Online (Sandbox Code Playgroud)
welcomedialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/texturebg"
android:id="@+id/layout_root"
android:orientation="vertical"
android:padding="40px">
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
注:我已经尝试使用FrameLayout为根ViewGroup,而不是LinearLayout为每一个建议,我发现某处-但这并没有帮助.
结果

public class Welcome extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
LayoutInflater inflater …Run Code Online (Sandbox Code Playgroud)