2017-10-14 92 views
-3

我現在正在自學android,並嘗試使用Java做一個筆記應用程序。將動態圖像添加到編輯文本中

我的問題是我不能將圖像動態添加到編輯文本中。儘管我已經搜索了很多,但我仍然沒有辦法做到這一點。

請幫助我,至少關於這個想法。

下圖說明了我正在努力解決的問題。

demo

+0

我沒有得到這個問題。 –

+0

爲什麼要將圖像添加到EditText中? 你想用短信和繪圖功能創建一個筆記應用程序嗎?你想在EditText上繪畫? – FarshidABZ

+0

我正在做一個筆記應用程序的功能。每當我使用相機拍攝照片時,照片將被動態設置爲編輯文本。希望你得到它。 –

回答

-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); 
} 

}

和La YOUT爲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"/> 

+0

這是作爲答案發布的,但它不會試圖回答這個問題。它應該可能是一個評論或完全刪除。 –

+0

問題是我不知道每張紙條上會有多少張照片。因此,在xml中添加imageview以上的編輯文本將不是好主意。希望我的想法正確 –

+0

而不是一個imageview只使用linearlayout(上面的edittext)並動態添加子項。 – KrzysztofB

0

使用

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> 
+0

'setCompoundDrawablesWithIntrinsicBounds'做什麼?當我使用相機拍攝照片時,它將不在mipmap目錄中。 –

+0

您可以在XML代碼中的EditText上添加ImageView。 – KeLiuyue

+0

在xml中定義imageview將限制我可以使用相機拍攝的照片。例如,在你的xml代碼中,我只能設置一次圖像(根據我的理解)。我真正想要的是每次使用相機拍照時,我都可以添加照片來編輯文本 –

0

謝謝你的支持。感謝你們,我發現了動態添加圖像的方式(不是編輯文本,對於我的誤解抱歉)

這裏是我需要它的人的源代碼。我的代碼是一團糟,但它的工作原理。

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"); 
      } 
     } 
    } 

} 
相關問題