0

当用户按下按钮时,我希望片段 B 出现在左侧,如下所示:

在此处输入图像描述

这是我现在执行此操作的代码:

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

<ViewGroup
    android:id="@+id/leftContainer"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#ffff0000" />

<ViewGroup
    android:id="@+id/rightContainer"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#ff00ffff" />

</LinearLayout> 

android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.leftContainer, fragmentA);
ft.add(R.id.rightContainer, fragmentB);
ft.commit();

不幸的是,这一切给我的只是黑屏。我什至看不到片段的背景颜色。

4

3 回答 3

1

您可以在添加 B 和 A 的情况下开始布局,但将 B 的可见性设置为“GONE”吗?然后,当您希望 B 出现时,更改其可见性。

如果我这样做,我会将它们都放在一个 LinearLayout 中。我将 B 的宽度设置为 0dp 并将其 layout_weight 设置为 1。我将 A 的宽度设置为 0dp 并将其 layout_weight 设置为 2。当 B 消失时,A 将填充整个布局,当 B 出现时,您将得到一个布局1/3 B 和 2/3 A。

ETA:这里有一些代码:

资源/布局/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topLevel"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    >
  <TextView  
      android:id="@+id/B"
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="fill_parent" 
      android:text="B"
      android:background="#f00"
      android:textSize="36sp"
      android:gravity="center"
      android:visibility="gone"
      />
  <TextView  
      android:id="@+id/A"
      android:layout_width="0dp" 
      android:layout_weight="2" 
      android:layout_height="fill_parent" 
      android:text="A"
      android:background="#00f"
      android:textSize="36sp"
      android:gravity="center"
      />
</LinearLayout>

FooApp.java:

package org.efalk.fooapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class FooApp extends Activity implements View.OnClickListener {
    private TextView A, B;
    boolean gone = true;

    @Override
    public void onCreate(Bundle savedState)
    {
        super.onCreate(savedState);
        setContentView(R.layout.main);
        A = (TextView)findViewById(R.id.A);
        B = (TextView)findViewById(R.id.B);
        A.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        gone = !gone;
        B.setVisibility(gone ? View.GONE : View.VISIBLE);
    }
}
于 2012-07-23T19:46:08.333 回答
0

您需要以 xml 或其他方式创建一个布局,以用作这些片段的容器。然后在fragmenttransaction中简单的添加fragment A。然后,您的布局中将有一些视图组,您可以将片段 A 添加到其中,并且您需要指定要将片段添加到事务中的哪个视图组。此外,您需要确保在事务上调用 commit 以使您的更改生效。如果您正在寻找一些不错的过渡等,那么您需要更多地考虑如何构建您的布局以及添加片段的逻辑等。

于 2012-07-23T18:21:16.977 回答
0

答案是:

add B
add A
call ft.hide(B);

然后在需要时:

call ft.show(B);
于 2012-07-24T13:26:20.490 回答