Android滑动菜单更改页面上的内容可以这样做吗?

时间:2011-10-25 01:05:38

标签: android

主页面顶部有一个horizo​​ntalScrollView,其中有许多TextView文本字段。我希望能够滚动并单击TextView文本上的任何内容,它会更改RelativeLayout以匹配Horizo​​ntalScrollView中的选择。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_height="wrap_content" android:scrollbars="none" android:layout_width="wrap_content">
    <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" android:id="@+id/linearLayout1">
        <TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="@+id/EditTOne" android:text="tOne"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="@+id/EditTTwo" android:text="tTwo"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="@+id/EditTThree" android:text="tThree"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="@+id/EditTFour" android:text="tFour"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="@+id/EditTFive" android:text="tFive"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="@+id/EditTSix" android:text="tSix"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="@+id/EditTSeven" android:text="tSeven"></TextView>
    </LinearLayout>
</HorizontalScrollView>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/relativeLayout1"></RelativeLayout>
</LinearLayout>

我有一个名为tone的第二个布局页面,它将链接到Horizo​​ntalScrollView中的菜单选项tOne。

tone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is tOne"></TextView>    
</LinearLayout>

如果通过写入LogCat选择前4个选项之一,主活动将标记。如果用户单击Horizo​​ntalScrollView中的tOne选项,是否可以在relativelayout中显示tone.xml?然后,如果用户在RelativeLayoutView等中选择tTwo display ttwo.xml?

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SlideMenu extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    TextView tOne, tTwo, tThree, tFour, tFive, tSix, tSeven, tEight, tNine, tTen;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        RelativeLayout vSpace = (RelativeLayout)findViewById(R.id.relativeLayout1);

        tOne = (TextView)findViewById(R.id.EditTOne);
        tTwo = (TextView)findViewById(R.id.EditTTwo);
        tThree = (TextView)findViewById(R.id.EditTThree);
        tFour = (TextView)findViewById(R.id.EditTFour);
        tFive = (TextView)findViewById(R.id.EditTFive);
        tSix = (TextView)findViewById(R.id.EditTSix);
        tSeven = (TextView)findViewById(R.id.EditTSeven);



        tOne.setOnClickListener(this);
        tTwo.setOnClickListener(this);
        tThree.setOnClickListener(this);
        tFour.setOnClickListener(this);
        tFive.setOnClickListener(this);
        tSix.setOnClickListener(this);
        tSeven.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {

        //--------------
        case R.id.EditTOne:
            System.out.println("Text One pressed");

        break;
        case R.id.EditTTwo:
            System.out.println("Text Two pressed");
        break;
        case R.id.EditTThree:
            System.out.println("Text Three pressed");
        break;
        case R.id.EditTFour:
            System.out.println("Text Four pressed");
        break;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我相信这很简单。我添加了

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.tone, vSpace);

打印“Text One Pressed”后。请务必先将vSpace作为字段。

要对所有这些执行此操作,请致电

vSpace.removeAllViews()
在给每个视图充气之前