Java 如何在android中移动图像视图
在我的应用程序中,我想动态移动“plus”图像,当我点击plus图像时,它会出现在我在android中新生成的编辑文本框旁边,我的文本视图和编辑文本框都会动态出现在新行中查看我的图像。我想要一排 我的代码:Java 如何在android中移动图像视图,java,android,Java,Android,在我的应用程序中,我想动态移动“plus”图像,当我点击plus图像时,它会出现在我在android中新生成的编辑文本框旁边,我的文本视图和编辑文本框都会动态出现在新行中查看我的图像。我想要一排 我的代码: package com.test; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.v
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>
如果你的意思是在其他框上移动,那么制作一个根框架布局,在其中放置两个布局:在第一个布局中,RelativeLayout中只有你的加号,在第二个布局中,所有其他框。在按住鼠标的同时添加一个侦听器并通过它启动动画。由于加号位于它自己的图层中,因此可以根据需要移动它
“它必须从旧位置删除,当我单击新的编辑文本框时,它会出现在新的编辑文本框旁边。它会创建新的图像,并再次出现在新的编辑文本框旁边” 如果你只想让它在一个地方消失,在另一个地方出现,它就不必是同一个物体。使用一个图像创建两个不同的视图。根据需要,使其中一个可见,另一个不可见
注意使用LayoutParams时不要说它们来自哪个类。它粉碎了应用程序。该类应该是父布局的类。例如,LinearLayout.LayoutParams 你说的动态运动是什么意思?每一个动作都是动态的。意思是当我点击plus image时,它会给我一个新的Editext,但image plus会保持在相同的位置,我希望它在新的Editext框旁边。你希望它在新的位置还是在相同的位置?那你为什么称运动为动态的呢?它必须从原来的位置移除,当我点击那个加上图像时,它会出现在新的编辑文本框旁边,它会创建新的图像,再次出现在一个新的编辑文本框旁边,问你的代码,你把活动和布局连接起来,您可以在其中更改可见性和布局。现在有代码很好+我为此付出了代价。---通常,你会因为没有对代码提出质疑而在这里获得否决票,即使这个问题真的不需要任何代码阅读我编辑的答案的底部,我发现你的代码有问题。