2012-07-23 84 views
0

当用户按下一个按钮,我想B片段出现在左边,就像这样:片段添加到左侧

enter image description here

这里是我的代码现在要做到这一点:

<?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(); 

不幸的是,这一切给了我一个黑色的屏幕。我甚至没有看到片段的背景颜色。

+0

你试过有两个布局,并使用'的setContentView()'? – pqn 2012-07-23 19:37:28

+0

我还没有尝试过,但如果我不能提出更“优雅”的解决方案,我会尝试。谢谢 – Matt 2012-07-23 19:57:06

回答

0

答案是:

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

然后当:

call ft.show(B); 
0

您需要创建一个布局,使用xml或其他方式作为这些片段的容器。然后只需在fragmenttransaction中添加片段A.然后,您的布局中会有一些视图组,您可以将碎片A添加到您的布局中,并且您需要指定您希望将碎片添加到事务中的视图组。另外,您需要确保在事务中调用commit以使更改生效。如果你希望做一些漂亮的转换等,那么你需要考虑更多关于如何构造布局和添加片段的逻辑等。

+0

感谢您的回复。我编辑了OP,但现在我没有看到来自任何一个片段的内容(只是黑屏)。这更像你的建议吗? – Matt 2012-07-23 19:35:24

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.

埃塔:这里的一些代码:

RES /布局/ 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); 
    } 
} 
+0

不幸的是,这是行不通的。当B消失时,A不会调整大小以填满屏幕。我怀疑这与片段布置时的事实有关,它们的LinearLayout.LayoutParams切换到FrameLayout.LayoutParams,它不再携带重量。 – Matt 2012-07-23 20:15:28

+0

我附上了一些我相信做你想要的源代码。触摸“A”小部件以使“B”小部件来来去去。 – 2012-07-24 19:13:54

+0

嗯。你写了“片段”,我读了“小部件”。对于那个很抱歉。我不知道我的答案对你有什么好处。 – 2012-07-25 01:29:16