设为首页 加入收藏

TOP

Android之Fragment详解(非原创)(五)
2019-09-01 23:13:38 】 浏览:131
Tags:Android Fragment 详解 原创
" android:layout_toRightOf="@id/tab_menu_setting" android:visibility="gone" android:src="@mipmap/partner_red" /> </RelativeLayout> </LinearLayout> </LinearLayout> <View android:id="@+id/div_tab_bar" android:layout_width="match_parent" android:layout_height="2px" android:layout_above="@id/ly_tab_menu" android:background="@color/div_white" /> <FrameLayout android:id="@+id/ly_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/div_tab_bar" android:layout_below="@id/ly_top_bar"/> </RelativeLayout>

编写Fragment界面布局以及类
fg_my.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <Button android:id="@+id/btn_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一个显示信息"/> <Button android:id="@+id/btn_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个显示信息"/> <Button android:id="@+id/btn_three" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第三个显示信息"/> <Button android:id="@+id/btn_four" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第四个显示信息"/> </LinearLayout> 

自定义的Fragment类,这里的话我们通过getActivity.findViewById()来获得Activity 中的小红点,这里仅仅是简单的控制显示而已! MyFragment.java代码如下所示:

public class MyFragment extends Fragment implements View.OnClickListener{ private Context mContext; private Button btn_one; private Button btn_two; private Button btn_three; private Button btn_four; public MyFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_my,container,false); //UI Object btn_one = (Button) view.findViewById(R.id.btn_one); btn_two = (Button) view.findViewById(R.id.btn_two); btn_three = (Button) view.findViewById(R.id.btn_three); btn_four = (Button) view.findViewById(R.id.btn_four); //Bind Event btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); btn_four.setOnClickListener(this); return view; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_one: TextView tab_menu_channel_num = (TextView) getActivity ().findViewById(R.id.tab_menu_channel_num); tab_menu_channel_num.setText("11"); tab_menu_channel_num.setVisibility(View.VISIBLE); break; case R.id.btn_two: TextView tab_menu_message_num = (TextView) getActivity ().findViewById(R.id.tab_menu_message_num); tab_menu_message_num.setText("20&qu
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Android】组件化开发的简单应用 下一篇ContentProvider和ContentResolve..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目