关于xml布局中Fragment与 FrameLayout的理解

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".ActionTab_Activity" >

    <!-- 一定要tool:comtext -->
    <!--如果是fragment,一定要设置某fragment名字
   如 android:name="com.cohere.fc.FragmentProcess" 
    表示启动时先加载某fragment
    -->


    <Fragment
        android:id="@+id/main_titles"
        android:name="com.cohere.fc.FragmentProcess" 
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />

<pre name="code" class="html">    <!-- FrameLayout是一个放置Fragment的容器 ,
不需要指定某个Fragment的名字,
可以通过FragmentManager动态添加删除想放入其中的Fragment-->
<FrameLayout android:id="@+id/main_details" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="4" /></LinearLayout>
 


FrameLayout与Fragment标签都是用来放Fragment的,区别是,Fragment标签只能设置固定的Fragment,而FrameLayout中可以通过FragmentManager更换Fragment

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Fragment的步骤如下: 1. 创建Fragment类:创建一个继承自Fragment的类,并重写onCreateView()方法,在该方法返回Fragment所对应的布局文件。 2. 创建布局文件:创建一个对应Fragment布局文件,该布局文件可以包含其他布局元素,如TextView、EditText等。 3. 在Activity添加Fragment:在Activity的布局文件添加一个FrameLayout,用于容纳Fragment。然后在Activity获取FragmentManager,通过调用beginTransaction()方法开启一个事务,调用add()方法将Fragment添加到FrameLayout,并调用commit()方法提交事务。 4. 在Fragment添加逻辑代码:在Fragment添加逻辑代码,如处理用户输入、响应用户操作等。 示例代码如下: 1. 创建Fragment类 ```java public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); return view; } } ``` 2. 创建布局文件 my_fragment.xml ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" /> </LinearLayout> ``` 3. 在Activity添加Fragment ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.add(R.id.fragment_container, new MyFragment()); transaction.commit(); } } ``` activity_main.xml ```xml <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 4. 在Fragment添加逻辑代码 ```java public class MyFragment extends Fragment { private TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); mTextView = (TextView) view.findViewById(R.id.text_view); mTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "Hello, World!", Toast.LENGTH_SHORT).show(); } }); return view; } } ``` 以上是使用Fragment的基本步骤,希望能够对你有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值