2012-08-01 55 views
3

我认为标题覆盖了它:)我有一个可绘制对象,用于获取我可以用手势操纵的.png照片。我也有一个背景图像的xml布局,应该在这个可绘制对象后面。一切都发生在一个片段中。在Android片段中的布局视图中放置可绘制对象

当我运行代码并得到这个片段时,显示png和手势工作,但是,没有膨胀的布局和后退按钮按应用程序崩溃(我猜这是因为我在使用setContentView片段,所以没有后退堆栈?我如何避免这种情况?)。

稍后我会将其他图层添加到场景中。

我的问题是,我怎样才能用xml布局膨胀片段,在它上面显示drawable,以及以后可以在所有上面添加其他视图?

代码是这样对该片段:

public class RoomFragment extends Fragment { 

ViewGroup mRoot; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    mRoot = (ViewGroup) inflater.inflate(R.layout.room_fragment, null); 

    /** Placing furniture .png element in SandboxView */ 
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
      R.drawable.furniture); 
    View view = new SandboxView(this.getActivity(), bitmap); 
    this.getActivity().setContentView(view); // Replace with inflater? 

    return mRoot; 

}} 

谢谢!

回答

1

我解决了这个问题,在这个片段的LinearLayout包装中添加一个ID,然后在使用它的xml给片段充气后,用addView(view)在其中添加一个可绘制视图的实例。

片段XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/bitmapBox" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/room" 
    android:gravity="bottom|center" 
    android:orientation="vertical" > 
</LinearLayout> 

件的片段代码:

ViewGroup mRoot; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    mRoot = (ViewGroup) inflater.inflate(R.layout.room_fragment, null); 

    /** placing furniture element in SandboxView */ 
    LinearLayout myLayout = (LinearLayout) mRoot 
      .findViewById(R.id.bitmapBox); 

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
      R.drawable.furniture); 

    View view = new SandboxView(this.getActivity(), bitmap); 

    myLayout.addView(view); 

    return mRoot;