2012-03-30 166 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); 
     } 
    } 
}