2012-03-30 170 views
0

我可以在一个frameLayout内插入两个frameLayout吗? 我试过这段代码,它不工作!在另一个框架布局内插入框架布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 

    <FrameLayout 
     android:id="@+id/titles" 
     android:layout_width="0px" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 
    <FrameLayout 
      android:id="@+id/details" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="2" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:baselineAligned="false" 
      android:orientation="horizontal" > 

      <FrameLayout 
       android:id="@+id/details1" 
       android:layout_width="0px" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 

      <FrameLayout 
       android:id="@+id/details2" 
       android:layout_width="0px" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 
     </LinearLayout> 
    </FrameLayout> 


</LinearLayout> 

当我点击一个元素,我希望detail1被打印。我想细节1已经在细节下创建,所以我看不到它?

任何人都可以帮忙吗?

有什么,我想: enter image description here

enter image description here

回答

1

下面是答案:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    Top-level content view for the layout fragment sample. This version is 
    for display when in landscape: we can fit both titles and dialog. 
--> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:baselineAligned="false" 
     android:orientation="horizontal" > 

     <FrameLayout 
      android:id="@+id/titles" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <FrameLayout 
      android:id="@+id/details" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="2" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:baselineAligned="false" 
     android:orientation="horizontal" > 

     <FrameLayout 
      android:id="@+id/titles1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 

     <FrameLayout 
      android:id="@+id/details1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <FrameLayout 
      android:id="@+id/details2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</RelativeLayout> 

我得到了很好的效果:) 感谢每一个机构帮助,th anks

0

,如果你对所有宽度0像素,你有什么打算看呢?并且不要使用px,请使用dip。我不知道你想什么来实现的,反正

[删除我的答案]

+0

这不能工作:<的FrameLayout 机器人:ID = “@ + ID /方式” 机器人:layout_width = “match_parent” 机器人:layout_height = “match_parent”> 机器人:layout_width = “match_parent” 使我在所有屏幕上的框架布局,android:layout_weight =“2”将不起作用 – 2012-03-30 16:35:48

+0

为什么不画一些草图并在这里张贴让我们看看你想要什么。 – 2012-03-30 17:09:12

+0

我已经加入图片到我的问题 – 2012-03-31 12:25:13

0

我觉得现在的问题是,你还没有完全了解如何使用layout_weight。而且您必须更好地跟踪与设计中的不同layout_weights相关的视图的哪个家庭(父母和子女)。

我不完全确定事情应该如何工作,但我认为你希望左侧菜单始终可见,然后根据用户选择的动态显示更多细节。您还希望地图始终能够部分显示?

把上面的代码放在一起。玩弄代码,你可能会理解它是如何工作的。如果您在理解代码时遇到问题,或者我误解了您的想法,请告诉我。

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1.0" 
     android:orientation="vertical" 
     android:background="@android:color/white" 
    > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Menu" 
      android:textColor="@android:color/black" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Show Details" 
      android:onClick="showdetails" /> 

    </LinearLayout> 

    <FrameLayout 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="2.0" 
    > 
     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/ic_launcher" /> 

     <LinearLayout 
      android:id="@+id/DetailsLinearLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:visibility="gone" 
     > 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1.0" 
       android:text="Details" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="@android:color/black" 
       android:background="@android:color/white" /> 

      <!-- This view is only here to prevent textView2 from completly 
       covering the underlying picture --> 
      <View 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1.0" /> 
     </LinearLayout> 

    </FrameLayout> 

</LinearLayout> 

的Java

package com.test.insertframelayoutinsideanotherframelayout; 

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

public class InsertframelayoutinsideanotherframelayoutActivity extends Activity { 

    LinearLayout detailsLinearLayout; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     detailsLinearLayout = (LinearLayout) findViewById(R.id.DetailsLinearLayout); 
    } 

    public void showdetails(View v){ 
     if (detailsLinearLayout.getVisibility() == View.GONE){ 
      detailsLinearLayout.setVisibility(View.VISIBLE); 
     } 
     else { 
      detailsLinearLayout.setVisibility(View.GONE); 
     } 
    } 
}