【问题标题】:I want to use same xml file with two different fragments我想使用具有两个不同片段的相同 xml 文件
【发布时间】:2016-06-04 14:20:49
【问题描述】:

我是安卓编程新手。我想编写一个包含两个片段的android程序。设计将是相同的,但我不能两次编写布局文件,因为它有很多小部件,所以有没有办法处理这个问题?

如果我复制 xml 文件小部件 ID 保持不变,我无法在我的 java 类中访问它们...

【问题讨论】:

  • 您是否在每个片段中使用exact 相同的 XML 布局?
  • 为什么不能在两个 Fragment 中扩展同一个 xml 文件?

标签: java android xml android-fragments android-studio


【解决方案1】:

假设你有 fragment_container.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fraContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />

你需要做的是用你的程序中的一个片段替换这个容器,例如:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_container);

    if (findViewById(R.id.fraContainer) != null) 
    {
        MyFragmentA myFragment = new MyFragmentA();        
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fraContainer, myFragment)
                .commit();
    }
}

这会将片段布局(容器)替换为您想要的片段。

现在让我们检查 MyFragmentA 和 MyFragmentB

public class MyFragmentA extends Fragment {

   ...

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   {
      View view = inflater.inflate(R.layout.fragment_both, container, false);
      TextView x = (TextView)view.findViewById(R.id.textView);
      x.setText("I am fragment A");
      return view;
   }
   ...

还有第二个片段

public class MyFragmentB extends Fragment {

   ...

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   {
      View view = inflater.inflate(R.layout.fragment_both, container, false);
      TextView x = (TextView)view.findViewById(R.id.textView);
      x.setText("I am fragment B");
      return view;
   }
...

通过引用视图对象,都膨胀了相同的fragment_both.xml,并且都使用相同ID的相同TextView!

希望能帮助你理解。

【讨论】:

    【解决方案2】:

    您可以在更多片段中简单地使用相同的 xml 文件,如下所示:

    片段 A

    public class FragmentA extends Fragment {
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate(R.layout.your_layout, container, false);
    
            ...
    
            return v;
        }
    }
    

    片段 B

    public class FragmentB extends Fragment {
         @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Inflate the layout for this fragment
                View v = inflater.inflate(R.layout.your_layout, container, false);
    
                ...
    
                return v;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      相关资源
      最近更新 更多