【问题标题】:Add a fragment to the left side在左侧添加一个片段
【发布时间】:2012-07-23 18:15:24
【问题描述】:

当用户按下按钮时,我希望片段 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();

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

【问题讨论】:

  • 您是否尝试过使用两种布局并使用setContentView()
  • 我还没有尝试过,但如果我不能想出更“优雅”的解决方案,我会尝试一下。谢谢

标签: android view fragment


【解决方案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:这里有一些代码:

res/layout/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);
    }
}

【讨论】:

  • 不幸的是,这不起作用。当 B 离开时,A 不会调整大小以填满屏幕。我怀疑这与布局片段时,它们的 LinearLayout.LayoutParams 切换到不再承载重量的 FrameLayout.LayoutParams 的事实有关。
  • 我附上了一些我相信你想要的源代码。触摸“A”小部件以使“B”小部件来来去去。
  • 嗯。你写了“片段”,我读了“小部件”。对于那个很抱歉。不知道我的回答对你有没有好处。
  • 是的,我认为这会起作用,但不幸的是片段有点奇怪。将可见性设置为消失不会通知其他片段调整大小。你必须调用 FragmentTransaction.hide(B)
【解决方案2】:

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

【讨论】:

  • 感谢您的回复。我编辑了 OP,但现在我看不到任何一个片段的内容(只是黑屏)。这更像你的建议吗?
【解决方案3】:

答案是:

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

然后在需要时:

call ft.show(B);

【讨论】:

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