java如何在安卓中移动图像视图
在我的应用程序中,我想动态移动“plus”图像,当我点击plus图像时,它会出现在我在安卓中新生成的编辑文本框旁边,我的文本视图和编辑文本框都会动态出现在新行中查看我的图像。我想要一排
我的代码:
package com.test;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.text.Editable;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.view.ViewGroup.LayoutParams;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ImageView;
import 安卓.widget.LinearLayout;
import 安卓.widget.TextView;
import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical"
安卓:id="@+id/linearlayout">
<LinearLayout
安卓:id="@+id/linearLayout2"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:background="#00ffff">
<ImageView
安卓:id="@+id/imageView1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:src="@drawable/plus" />
</LinearLayout>
<ScrollView
安卓:id="@+id/scrollView1"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<LinearLayout
安卓:id="@+id/linearLayout1"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
# 1 楼答案
如果你的意思是在其他框上移动,那么做一个根框架布局,在其中放置两个布局:第一个布局中,RelativeLayout中只有你的加号,第二个布局中是所有其他框。在plus Touch上添加一个监听器并通过它启动动画。因为加号在它自己的图层中,你可以随心所欲地移动它
“它必须从旧位置删除,当我单击新的编辑文本框时,它会出现在新的编辑文本框旁边。它会创建新的图像,并再次出现在新的编辑文本框旁边。”
如果你只想让它在一个地方消失,在另一个地方出现,它不必是同一个物体。用一个图像创建两个不同的视图。根据需要,使其中一个可见,另一个不可见
注意使用LayoutParams时不要说它们来自哪个类。它粉碎了应用程序。类应该是父布局的类。例如,线性布局。布局参数