动态添加图像到编辑文本中

时间:2017-10-14 13:07:46

标签: java android android-edittext

我现在正在自学Android并尝试使用Java做笔记应用程序。

我的问题是我无法动态地将图像添加到编辑文本中。尽管我已经对它进行了大量搜索,但我仍然没有办法实现它。

请帮助我,至少关于这个想法。

下图说明了我正在努力解决的问题。

demo

3 个答案:

答案 0 :(得分:1)

谢谢你的支持。谢谢你们,我发现了动态添加图片的方式(不是编辑文本,对不起我的误解)

这是我需要它的人的源代码。我的代码很乱,但它确实有用。

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Move to Camera" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    LinearLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnMove = (Button) findViewById(R.id.button);
        layout = (LinearLayout) findViewById(R.id.activity_main);

        btnMove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,113);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == 113 && resultCode == RESULT_OK) {
                Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layout.setLayoutParams(params);
                setContentView(layout);
                layout.setOrientation(LinearLayout.VERTICAL);
                ImageView imgView = new ImageView(this);
                imgView.setLayoutParams(params);
                layout.addView(imgView);
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.postRotate(90);
                Bitmap image = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                imgView.setImageBitmap(image);
                EditText edtText = new EditText(this);
                edtText.setLayoutParams(params);
                edtText.setBackground(null);
                layout.addView(edtText);
                Button btn = new Button(this);
                LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(btnParams);
                btn.setText("Move to Camera");
            }
        }
    }

}

答案 1 :(得分:0)

USE

  

setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

editText.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.you_image,0,0);

您可以在xml代码中的ImageView上方添加EditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<ImageView
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="content" />
</LinearLayout>

答案 2 :(得分:-1)

我认为它不可能开箱即用。只需在EditText上方添加ImageView即可。或者更好的是,使用EditText和LinearLayout(用于动态添加的视图)创建自定义视图。

这样的事情(请记住它的伪代码):

<强>活动:

public class MainActivity extends AppCompatActivity {

CustomView customView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    customView = findViewById(R.id.custom_view);

    someAction()
}

public void someAction() {
    customView.addImage(...);
}

自定义视图

public class CustomView extends FrameLayout {

private LinearLayout dynamicViewsParent;

public CustomView(@NonNull Context context) {
    super(context);
    init();

}

public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    View inflatedView = View.inflate(getContext(), R.layout.custom_view, this);
    dynamicViewsParent = inflatedView.findViewById(R.id.dynamicViewsWrapper);
}

public void addImage(SomeKindOfSource source) {
    View view = createViewFrom(source);
    dynamicViewsParent.addView(view);
}

}

CustomView

的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/custom_view">

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

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TEXT"/>