android的title使用了fragment,为什么标题显示不到标题栏上呢?

在MainActivity中,尽管使用了fragment定义标题,但标题没有显示在标题栏上。代码显示在onCreate()方法中设置了Window.FEATURE_NO_TITLE,这可能导致标题不显示。要解决此问题,需要移除或修改该设置,确保fragment的标题能够正常显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我将标题部分使用了fragment定义,但是在模拟器运行的时候,发现我定义的标题没有在android的标题部分显示,模拟器运行效果如图:,应该怎么定义才能使标题部分的fragment放到标题栏上呢?
相关代码如下:
MainActitu.java:
[/size]public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }
}
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context="com.example.liwenchao.webuidemo.MainActivity">



    <fragment
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:name="com.example.liwenchao.webuidemo.TitleFragment"
        android:id="@+id/titleFragment"/>


    <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.example.liwenchao.webuidemo.ContentFragment"
        android:id="@+id/contentFragment"/> 
</RelativeLayout>
TitleFragment.java:


public class TitleFragment extends Fragment {
    private ImageButton imageButton;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.title_fragment,container,false);


        imageButton= (ImageButton) view.findViewById(R.id.left);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"第一个Fragment",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}

title_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageButton
        android:id="@+id/left"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"/>
    <TextView
        android:text="我不是微信"
        android:textColor="#fff"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值