[英]How to move image view in android
在我的应用程序中,当我单击加号图像时,我想动态移动“加号”图像,它位于android中我新生成的EditText框旁边,而我的textview和edittext框都动态出现在新行中,可以看到我的图像。 我要单行。
我的代码:
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>
如果要在其他框上四处移动,请制作一个根FrameLayout,并在其中放置2个布局:第一个在RelativeLayout中只有您的加号,第二个在所有其他框中。 在加触摸上,添加一个侦听器并由此启动动画。 由于加号位于其自己的图层中,因此您可以根据需要移动它。
“当我单击该图像时,它必须从旧的位置中移出并出现在新的EdiText框旁边,它会创建新图像,并再次出现在新的EdiText框旁边”
如果只希望它在一个地方消失而在另一个地方出现,则不必是同一对象。 用一张图像制作两个不同的视图。 并根据需要使其中一个可见而其他不可见。
当心使用LayoutParams,而不要说它们来自哪个类。 它粉碎了应用程序。 该类应该是父布局的类。 例如,LinearLayout.LayoutParams。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.