2017-10-14 11 views
-3

私は今、自己学習しているアンドロイドで、Javaを使ってノートアプリケーションを作成しようとしています。テキストを編集テキストに動的に追加する

私の問題は、画像を編集テキストに動的に追加できないことです。私はそれについて多くのことを探していましたが、それを作る方法はまだありません。

少なくとも私の考えを助けてください。

次の図は、私が苦労している問題を示しています。

demo

+0

私は問題を取得できませんでした。 –

+0

EditTextに画像を追加したいのはなぜですか? テキストと描画機能を備えたノートアプリケーションを作成しますか? EditTextにペイントしたいですか? – FarshidABZ

+0

私はノートアプリケーションのfuntionalityをやっています。カメラを使用して写真を撮るたびに、写真は動的に編集テキストに設定されます。あなたがそれを得ることを願っています。 –

答えて

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

}

とラ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

1つのイメージビューの代わりに、インスタンス(例えば上記のedittext)にlinearlayoutを使用し、子を動的に追加してください。 – KrzysztofB

0

使用

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

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

OR

EditTextの上にImageViewを追加することができます。

<?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'は何をしますか?カメラを使って写真を撮影すると、ミップマップディレクトリには表示されません。 –

+0

ImageTextをxmlコードのEditTextの上に追加できます。 – KeLiuyue

+0

xmlでimageviewを定義すると、カメラを使って撮ることができる写真が削除されます。例えば、あなたのXMLコードでは、(私の理解のために)画像を1回だけ設定することができました。私が本当に欲しいのは、カメラを使用して写真を撮るたびにテキストを編集するための写真を追加できるということです –

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

} 
関連する問題