动态更改活动的布局

时间:2013-08-19 05:49:08

标签: android viewflipper android-framelayout

我正在处理Tabular数据。我的屏幕有3个标签。所有三个选项卡都是一个活动的一部分。每个标签都有不同的布局。

我的要求是其中一个标签的布局可以动态更改。现在因为它是活动的一部分,我无法创建另一个活动。创建另一个活动将影响其他选项卡。

基本上,我想知道如何改变动态屏幕的布局。我应该使用ViewFlipper还是Framelayout?

3 个答案:

答案 0 :(得分:0)

为什么你没有使用这些片段,请尝试以下链接可以帮助你..

Android Fragment Basics

Android Fragment Example

答案 1 :(得分:0)

我建议你使用viewpager,actionbar(sherlock actionbar for lower version)和片段。 以下是一些例子

  1. official Document for ViewPager

  2. androidsolution4u

  3. android-er.blogspot

答案 2 :(得分:0)

是的,有可能:

像这样编写你的xml文件:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#262626"
tools:context=".MainActivity" >
<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  //design for 1st tab

</LinearLayout>
<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  //design for 2nd tab

</LinearLayout>
<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  //design for 3rd tab

</LinearLayout>
<LinearLayout
    android:id="@+id/footer_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#acacac"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" >

            <Button
                android:id="@+id/1stTab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="1dp"/>

            <Button
                android:id="@+id/2ndTab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"
                android:layout_toRightOf="@+id/1stTab"
                 />

            <Button
                android:id="@+id/3rdTab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"
                android:layout_toRightOf="@+id/2ndTab"/>

        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>
</RelativeLayout>

现在在.java文件中执行以下操作:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View layout1 = (View) findViewById(R.id.linearLayout1);
    View layout2 = (View) findViewById(R.id.linearLayout2);
    View layout3 = (View) findViewById(R.id.linearLayout3);
    Button menu1 = (Button) findViewById(R.id.1stTab);
    Button menu2 = (Button) findViewById(R.id.2ndTab);
    Button menu3 = (Button) findViewById(R.id.3rdTab);
    layout3.setVisibility(View.GONE);
    layout2.setVisibility(View.GONE);
    layout1.setVisibility(View.VISIBLE);
    menu1.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            layout2.setVisibility(View.GONE);
            layout3.setVisibility(View.GONE);               
            layout1.setVisibility(View.VISIBLE);
     //do your rest tabbing work of 1stTab
        }
    });
     menu2.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            layout1.setVisibility(View.GONE);
            layout3.setVisibility(View.GONE);               
            layout2.setVisibility(View.VISIBLE);
     //do your rest tabbing work of 2ndTab
        }
    });
     menu3.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            layout1.setVisibility(View.GONE);
            layout2.setVisibility(View.GONE);               
            layout3.setVisibility(View.VISIBLE);
       //do your rest tabbing work of 3rdTab
        }
    });
 }

这可能会对你有帮助。

相关问题