2013-11-28 100 views
1
布局

请帮我解决这个问题:麻烦与我的应用程序

http://s27.postimg.org/bp1txpnoj/Capture.png

XML代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout2" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Main_Page" > 

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="16dp" 
    tools:listitem="@android:layout/simple_spinner_dropdown_item" /> 

</LinearLayout> 

这是我的应用程序的代码的main.xml。代码已从“相对”改为“LinearLayout”

+5

请发表您的布局代码 – kai

+0

所以你添加通过代码的按钮,你的活动?这和你发布的图片不一样。 – kai

+0

是的,因为当我从微调框中选择不同的选项时,组件也应该改变。如果你能建议一个更好的方法来做它,请让我知道。 –

回答

0

使用LayoutInflator并在选择微调器中的不同值时将视图充满到主视图。

//主要布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/main_layout_id"> 

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="16dp" 
    tools:listitem="@android:layout/simple_spinner_dropdown_item" /> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/insert_Layout"> 
</LinearLayout> 

</LinearLayout> 

//布局2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/layout_item_id"> 

    <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Hello, this is the inflated text :D" 
       android:layout_gravity="center" 
       android:gravity="center_horizontal" 
       android:id="@+id/text_item_id"/> 
</LinearLayout> 

//调用从XML

linearLayout inset_layout= (LinearLayout)findViewById(R.id.insert_Layout); 

主要布局/创建一个视图以膨胀layout_item(带有之前创建的textView的xml)

View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false); 

//添加到邮件布局

mainLayout.addView(view); 

希望这有助于:)

+0

这是连接多个布局的代码吗? –

+0

是的,代码会动态扩展主布局中的布局2.您可以在主布局中拥有多个占位符,并相应地扩展不同的布局。 http://developer.android.com/reference/android/view/LayoutInflater.html – Sujay

+0

thx老兄如果你不介意可以帮我用这个(http://stackoverflow.com/q/20280272/3026643) –