【问题标题】:Dynamically include another layout in Fragment Activity在 Fragment Activity 中动态包含另一个布局
【发布时间】:2014-03-23 14:29:51
【问题描述】:

这是我的 Home.xml 布局文件

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rellay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/placesgradient">

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="click" 
        />

    **<I want to include another layout here on a click of a button dynamically on click of a button >**

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
       android:layout_below="@id/btn2"
        android:text="hello this is text..see above" />

</RelativeLayout>

这是我的 Home.java 文件

public class Home extends Fragment implements OnClickListener {
    Button b1;
    RelativeLayout r1;
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.home, container, false);
        b1 = (Button) rootView.findViewById(R.id.btn2);
        b1.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View arg0) {
        ViewGroup con = null;
        r1 = (RelativeLayout) rootView.findViewById(R.id.rellay);
        LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        r1.addView(layoutInflater.inflate(R.layout.test1, con , false));

    }
}

当我单击按钮时,它会将 test1.xml 布局文件放在 home.xml 布局的顶部。我需要它位于按钮和文本之间。我之前在堆栈上检查过这段代码,但它似乎不适用于 FragmentActivity。这里 1 表示本例中的索引或第二个孩子。

rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );

我怎样才能实现它?救命!!

【问题讨论】:

    标签: android android-layout android-fragments layout-inflater xml-layout


    【解决方案1】:

    我会将一些容器视图放在您希望布局如下所示的位置:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/rellay"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:background="@drawable/placesgradient">
    
        <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="click"
                />
    
        <FrameLayout 
                android:id="@+id/flContainer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_below="@id/btn2"
                android:text="hello this is text..see above" />
    
    </RelativeLayout>
    

    然后您可以像这样在 Fragment 中检索容器:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.home, container, false);
    
        // Retrieve your container
        flContainer = rootView.findViewById(R.id.flContainer);
    
        b1 = (Button) rootView.findViewById(R.id.btn2);
        b1.setOnClickListener(this);
    
        return rootView;
    }
    

    稍后当您想要添加布局时,您可以像这样将子元素添加到 FrameLayout

    flContainer.addView(subLayout);
    

    如果您以后想更改容器中的布局,可以这样做:

    flContainer.removeAllViews();
    flContainer.addView(otherLayout);
    

    【讨论】:

    • 有没有办法在 removeAllViews() 上添加动画?
    • 是的,只需在布局 xml 的根视图上设置 android:animateLayoutChanges="true"
    • 也可以执行自定义动画,如果您想让我告诉您如何执行,请在此处评论,但这比设置android:animateLayoutChanges="true"要多一些代码。
    • stackoverflow.com/questions/22615720/… 看看这个..我已经完成了一些部分,我需要在哪里更改
    【解决方案2】:

    使用布局参数。

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, R.id.btn2);
    
    r1.addView(layoutInflater.inflate(R.layout.test1, con , false),params);
    

    或者将视图放入您的布局中并使用View.setVisibility(View.GONE/View.VISIBLE) 切换可见性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多