Fragment使用中的一些疑惑。

我在学习Android第一行代码的时候按照书上第四章的代码练习的时候遇到一些疑惑。首先贴上主布局:

<?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">

   <fragment
       android:id="@+id/left_fragment"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:name="com.example.leslie.fragmenttest.LeftFragment"
       android:layout_weight="1"/>
    <!--<FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >-->
    <fragment
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.leslie.fragmenttest.RightFragment" />

</LinearLayout>

可以看出主布局很简单,就是一个线性布局下2个Fragment.下面贴出主活动中的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.button:
                AnotherRightFragment fragment=new AnotherRightFragment();
                FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction transaction=fragmentManager.beginTransaction();
                transaction.replace(R.id.right_fragment,fragment);
                transaction.commit();
                break;
            default:break;
        }

代码的逻辑也很简单,就是希望点击左边的fragment中的按钮,能够更换右边的fragment。为此制作了右边的2个fragment布局文件如下
这是right_fragment布局文件:

<?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:background="#00ff00">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment"/>

</LinearLayout>

这是another_right_fragment布局文件:

<?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:background="#ffff00"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20dp"
        android:text="This is another right fragment"/>

</LinearLayout>

两者并无太大不同。仅仅显示的text不同而已,运行结果并没有像我想象的那样右边的fragment完全覆盖住,而是仅仅覆盖了非TextView部分。
如图所示:运行结果
这让我百思不得其解。
网上搜索到的答案是用FrameLayout做被替代的fragment的父容器就可以了。我自己暂时还不知道为什么要这么做。为什么不用FrameLayout只能替代一部分导致还有一部分可见。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Fragment使用findViewById方法,需要先获取Fragment的根视图,然后再通过根视图调用findViewById方法来查找对应的视图控件。具体步骤如下: 1. 在Fragment的onCreateView方法,通过LayoutInflater的inflate方法将布局文件转换成View对象,并返回该View对象作为Fragment的根视图。 2. 在Fragment的onViewCreated方法,通过根视图调用findViewById方法来查找对应的视图控件,并将其赋值给成员变量。 例如,在Fragment查找一个TextView控件的代码如下: ``` public class MyFragment extends Fragment { private TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_layout, container, false); return rootView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mTextView = view.findViewById(R.id.text_view); } } ``` 在上面的代码,我们先通过LayoutInflater的inflate方法将布局文件fragment_layout转换成View对象,并将其作为Fragment的根视图返回。 然后,在onViewCreated方法,我们通过根视图view调用findViewById方法来查找id为text_view的TextView控件,并将其赋值给成员变量mTextView。这样,在Fragment的其他方法就可以直接使用mTextView来操作TextView控件了。 ### 回答2: 在Android开发Fragment是一种可以嵌入Activity的可重复使用的组件,它可以帮助我们将UI界面划分为多个模块,增强了UI的模块化和复用性。 在Fragment使用findViewById()方法可以查找对应视图文件的控件对象。 通常情况下,使用方式与Activity的findViewById()使用方式类似,在Fragment使用findViewById()方法需要调用该方法所在的视图对象,而不是调用Activity的方法。也就是说,在Fragment内部调用findViewById()方法时,需要先从onCreateView()方法获取Fragment的视图对象,然后再调用findViewById()方法,例如: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //获得视图对象 View view = inflater.inflate(R.layout.fragment_layout, container, false); // 从视图对象查找控件 Button button = (Button) view.findViewById(R.id.button); return view; } 在上面的代码,我们首先使用LayoutInflater将fragment_layout布局文件解析成为一个视图对象,然后使用这个视图对象查找到了其的Button控件,并进行赋值。需要注意的是,查找控件时必须在分配了布局资源之后操作,所以这里的findViewById()方法是在onCreateView()方法执行的。 综上所述,对于在Fragment使用findViewById()方法,我们需要先获取到Fragment的视图对象,然后再使用该对象来查找控件对象。这种方式可以帮助我们在Fragment更加灵活、方便地使用控件,实现更为精细的UI设计。 ### 回答3: 在Android开发Fragment是一种视图(或者说界面)的组成部分,可以多次重用。虽然Fragment有助于扩展应用程序,但是在Fragment使用“findViewById”方法可能会出现问题。因为Fragment通常不包含与Activity布局文件存在的视图层次结构完全匹配的整个UI界面。 然而,Fragment使用findViewById方法可以成功获取Activity UI指定的视图元素,因为此方法返回的是由Activity创建的视图层次结构的视图元素。另外一种方法是通过视图层次结构引用在Activity查找最近相邻的布局组件,然后获取到布局组件子视图组件的引用。 因此,在Fragment使用findViewById方法时,必须用一个特殊的技巧来确保方法执行良好。主要思想是使用布局组件的对象引用来调用findViewById方法,并从获取目标视图元素的引用。此外,在Fragment使用findViewById方法时必须注意在onCreateView方法调用它,而不能在onActivityCreated方法调用它。因为在onActivityCreated方法调用findViewById,可能会出现视图未关联或为空的情况。 总的来说,Fragment使用findViewById方法需要注意以下几点:一定要在onCreateView方法调用,需要使用布局组件的对象引用,最好在Fragment使用“getView”方法来获取父视图的引用,并在Fragment保存完整的视图层次结构,避免场景切换时引用丢失。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值