开发者

how to add layout on ontouch event of activity?

Here is my code that i am trying to add one layout at runtime on ontouch event. package 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;
            }
        });
    }   

}

and below is my 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>

i am adding the linearlayout of childview.xml on ontouch event. can anyone tell me what is the wrong with this code?


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

thankx


A bit tricky to debug, but I think when you are doing this:

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

You're getting NULL, because the id R.id.mainchield is not in your current contentview (setContentView(R.layout.main);)

You need to inflate it from the xml, something like this

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

And then you have your actual xml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜