如何在ontouch活动事件上添加布局?

时间:2011-01-25 14:29:20

标签: android android-layout

这是我的代码,我试图在运行时在ontouch事件上添加一个布局。 包DesingTesting.com;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class DesingTesting extends Activity 
{
    FrameLayout albumframe;
    LinearLayout extraview;
    LinearLayout mainchield;
    boolean flag = true;
    LayoutInflater inflater;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        setContentView(R.layout.main);

        albumframe = (FrameLayout)findViewById(R.id.llAlbumview);
        extraview = (LinearLayout)findViewById(R.id.extraview);

        inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mainchield=(LinearLayout)findViewById(R.id.mainchield);


        albumframe.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                if(flag)
                {
                    inflater.inflate(R.id.mainchield,extraview);
                    extraview.addView(mainchield);

                flag = false;

                }
                else
                {
                    flag = true;
                }

                return false;
            }
        });
    }   

}

以下是我的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"
    android:id = "@+id/mainlinear">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="50dp" android:background="@drawable/header_bg">

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

            <ImageButton android:id="@+id/btnBack"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="left" android:layout_marginLeft="10dp"
                android:background="@android:color/transparent" android:paddingTop="10dp"
                android:src="@drawable/back" />

            <LinearLayout android:layout_height="fill_parent"
                android:layout_width="wrap_content" android:layout_weight="1"
                android:gravity="center" android:orientation="vertical">

                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="CMX"
                    android:textColor="#919191" android:textSize="12sp"
                    android:textStyle="bold" android:singleLine="true" />

                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="Quanta"
                    android:textColor="#ffffff" android:textSize="12sp"
                    android:textStyle="bold" android:singleLine="true" />

                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="Talvikuningas"
                    android:textColor="#919191" android:textSize="12sp"
                    android:textStyle="bold" android:singleLine="true" />

            </LinearLayout>

            <ImageButton android:id="@+id/btnMore"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="right" android:src="@drawable/more"
                android:paddingLeft="4dp" android:paddingTop="10dp"
                android:paddingRight="10dp" android:background="@android:color/transparent" />

        </LinearLayout>

    </LinearLayout>

    <FrameLayout android:id="@+id/llAlbumview"
        android:layout_width="fill_parent" 
        android:layout_height="360dp"
        android:background="@drawable/chalte" 
        android:gravity="center"
        >
        <LinearLayout android:id="@+id/extraview"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="@android:color/transparent">
        </LinearLayout>

        <RelativeLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:paddingTop="315dp"
            android:background="@android:color/transparent">

            <LinearLayout android:orientation="horizontal"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:layout_gravity="bottom" android:id="@+id/l_playercntrl"
                android:background="@drawable/player_control_bg">

                <ImageButton android:id="@+id/imgprevious" android:src="@drawable/previous"
                    android:background="@android:color/transparent"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp" android:layout_marginRight="37dp" />

                <ImageButton android:id="@+id/imgpause"
                    android:background="@android:color/transparent"
                    android:layout_height="wrap_content" android:layout_width="wrap_content"
                    android:layout_marginRight="35dp" android:src="@drawable/play" />

                <ImageButton android:id="@+id/imgnext" android:src="@drawable/next"
                    android:background="@android:color/transparent"
                    android:layout_marginLeft="2dp" android:layout_height="wrap_content"
                    android:layout_width="wrap_content" />
            </LinearLayout>
        </RelativeLayout>

    </FrameLayout>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="center|bottom"
        android:paddingBottom="4dp">

        <SeekBar android:id="@+id/sbVolume" android:padding="4dip"
            android:progressDrawable="@drawable/skbar" android:maxHeight="9dp"
            android:thumb="@drawable/circle" android:thumbOffset="2dp"
            android:layout_width="300dp" android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

and here is childview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center|top" android:paddingBottom="4dp"
    android:background="@drawable/process_bg"
    android:orientation="vertical"
    android:id="@+id/mainchield">


    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="3 of 25"
        android:textColor="#919191" android:textSize="12sp" android:textStyle="bold"
        android:singleLine="true" />


    <SeekBar android:id="@+id/sbVolume" android:padding="4dip"
        android:progressDrawable="@drawable/skbar" android:maxHeight="9dp"
        android:thumb="@drawable/circle" android:thumbOffset="2dp"
        android:layout_width="300dp" android:layout_height="wrap_content" />

        <LinearLayout android:orientation="horizontal"
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"
                android:layout_gravity="bottom" 
                android:id="@+id/more"
                >

                <ImageButton android:id="@+id/imgprevious" android:src="@drawable/repite"
                    android:background="@android:color/transparent"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp" 
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="90dp" />

                <ImageButton android:id="@+id/imgpause"
                    android:background="@android:color/transparent"
                    android:layout_height="wrap_content" android:layout_width="wrap_content"
                    android:layout_marginRight="80dp" 

                    android:src="@drawable/circle_t" />

                <ImageButton android:id="@+id/imgnext" android:src="@drawable/saphal"
                    android:background="@android:color/transparent"
                    android:layout_marginLeft="2dp" android:layout_height="wrap_content"
                    android:layout_width="wrap_content" 
                    android:layout_marginTop="5dp"/>
            </LinearLayout>
</LinearLayout>

我在ontouch事件中添加了childview.xml的linearlayout。 任何人都可以告诉我这段代码有什么问题吗?

2 个答案:

答案 0 :(得分:2)

View view = inflater.inflate(R.id.mainchield,extraview,false); extraview.addView(视图);

thankx

答案 1 :(得分:0)

调试有点棘手,但我想当你这样做时:

  mainchield=(LinearLayout)findViewById(R.id.mainchield);

您收到NULL,因为您的当前内容视图(R.id.mainchield

中不存在ID setContentView(R.layout.main);

你需要从xml中膨胀它,就像这样

 inflatedView = (LinearLayout) View.inflate(this, R.layout.childview, null);

然后你有了你真正的xml。