本人将标题部分使用了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>
相关代码如下:
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>
解决方案
30
你最上面那个蓝的是toolbar吧。
不要本人写titlefragment啊,没有意义啊,直接用原生态的toolbar不行吗,有什么想不开的要本人写一个,还是你有什么效果是toolbar实现不了的?
不要本人写titlefragment啊,没有意义啊,直接用原生态的toolbar不行吗,有什么想不开的要本人写一个,还是你有什么效果是toolbar实现不了的?