2012-02-10 204 views
0

我想要动态地移动“加”图像当iI点击加上图像时,它出现在android中我新生成的EditText框旁边,我的textview和edittext框都动态地出现在新行看到我的形象。我想单排。如何在我的应用程序中移动图像视图android

我的代码:

package com.test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Test_testActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private LinearLayout mLayout; 
    private EditText mEditText; 
    private ImageView mButton; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mLayout = (LinearLayout) findViewById(R.id.linearLayout1); 
     mButton = (ImageView) findViewById(R.id.imageView1); 
     mButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //mButton.setVisibility(Button.GONE); 
       mLayout.addView(createEditText());    
       mLayout.addView(seconftext());  
      } 
     });  
    } 
    private EditText createEditText() 
    { 
     final LayoutParams lparams = new LayoutParams(150,100); // Width , height 
     final EditText edittext = new EditText(this); 
     edittext.setLayoutParams(lparams); 
     return edittext; 
    } 
    private TextView seconftext() 
    { 
     final LayoutParams iparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     final TextView tv = new TextView(this); 
     tv.setLayoutParams(iparams); 
     tv.setText("Second"); 
     return tv; 
    } 
} 

和.xml是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
     android:id="@+id/linearlayout"> 
    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="#00ffff"> 
     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/plus" /> 
    </LinearLayout> 
    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

enter image description here

+0

你是什么意思动态运动?每一个动作都是动态的。 – Gangnus 2012-02-10 11:29:37

+0

意思是当我点击加图像时,它给了我一个新的Editext,但图像加在同一个地方,我希望它在新的EdiText框旁 – 2012-02-10 11:31:15

+0

你想要一个新的地方或在同一个地方?你为什么称这个运动是动态的? – Gangnus 2012-02-10 11:34:52

回答

1

如果你的意思是围满了其他框的运动,使根的FrameLayout,把2点中的布局它:首先在第二个所有其他框中将只有您的RelativeLayout中的加号。加上触摸添加一个侦听器并通过它启动动画。由于加号位于其自己的图层中,因此可以根据需要将其移动。

“它必须从旧postition被移除,并配备旁新EdiText箱当我单击+图像它创造新的形象,再次来到了新EdiText框旁边的”

如果你只希望它消失一个地方出现在另一个地方,它不需要是同一个对象。用一个图像制作两个不同的视图。根据需要让其中一个可见和其他隐形。

小心使用LayoutParams,而不必说他们来自哪个类。它粉碎了应用程序。该类应该是父布局的类。例如,LinearLayout.LayoutParams。

+0

我试过但没有发生 – 2012-02-10 14:25:02

+0

把问题代码,将活动连接到布局,在此处更改可见性和布局。 – Gangnus 2012-02-10 14:35:54

+0

现在有代码是很好的。 +1。 ----通常情况下,你会在这里得到低估,因为不会把代码置疑,即使这个问题真的不需要任何代码。 ---在我编辑的答案的底部阅读,我在代码中看到一个问题。 – Gangnus 2012-02-16 16:36:52

相关问题