質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

6839閲覧

Android 文字と画像を重ねるLayoutにおいて,xmlでリソース定義する方法

退会済みユーザー

退会済みユーザー

総合スコア0

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2015/10/28 07:33

編集2015/10/28 08:07

Android Studioにて開発を行っています.Androidで,FrameLayoutを用いて重ねたImageViewとTextViewに対して,動的にリソースを指定したいです.しかし,今書いているコードでは,include文が必要になり,独自リソース定義を使うことができません.どういったコードを書けば,

  • 縦横比を問わず,Layout内いっぱいに画像を引き延ばす
  • 画像の中央に文字を重ねる
  • xmlから,リソース指定して画像やテキストを変更可能

という要件を満たせるのでしょうか.

最後に載せるソースコードが現状のコードになりますが,これらのコードでは,includeにてxmlに組み込むことになり,独自定義のリソースを認識していないようです.(nullになる.)

よろしくお願いします.

xml

1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <include layout="@layout/my_image_text_view" 8 android:id="@+id/cafeImageView" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 app:mySrc="@drawable/example" 12 app:myText="Example"/> 13</LinearLayout>

java/com.example.mine/view/MyImageTextView.java

Java

1package com.example.mine.view; 2 3/* import 文は省略*/ 4 5public class MyImageTextView extends FrameLayout { 6 7 private AttributeSet attrs; 8 9 public MyImageTextView(Context context, AttributeSet attrs) { 10 super(context, attrs); 11 this.attrs = attrs; 12 } 13 14 @Override 15 protected void onFinishInflate() { 16 super.onFinishInflate(); 17 init(); 18 } 19 20 private void init() { 21 final TextView textView = (TextView) findViewById(R.id.myTextView); 22 final ImageView imageView = (ImageView) findViewById(R.id.myImageView); 23 24 final TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyImageTextView); 25 26 final Drawable resourceId = typedArray.getDrawable(R.styleable.MyImageTextView_mySrc); 27 if(resourceId!=null) 28 imageView.setImageDrawable(resourceId); 29 30 final String text = typedArray.getString(R.styleable.MyImageTextView_myText); 31 textView.setText(text); 32 33 typedArray.recycle(); 34 } 35 36}

res/layout/my_image_text_view.xml

xml

1<com.example.mine.view.MyImageTextView xmlns:android="http://schemas.android.com/apk/res/android" 2 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content"> 6 7 <!--xmlns:app="http://schemas.android.com/apk/res-auto"--> 8 <ImageView 9 android:id="@+id/myImageView" 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:layout_gravity="center" 13 android:scaleType="fitXY" /> 14 15 <TextView 16 android:id="@+id/myTextView" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_gravity="center" 20 android:textAppearance="?android:attr/textAppearanceLarge" /> 21 22</com.example.mine.view.MyImageTextView>

res/values/attrs.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<resources> 3 <declare-styleable name="MyImageTextView"> 4 <attr name="mySrc" format="reference" /> 5 <attr name="myText" format="string" /> 6 </declare-styleable> 7</resources>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

xmlで書かずに,ソースコード内でinflateすることでとりあえず解決しました.

Java

1public class ImageTextView extends FrameLayout { 2 3 public ImageTextView(Context context) { 4 super(context); 5 init(context); 6 } 7 8 public ImageTextView(Context context, @Nullable AttributeSet attrs) { 9 super(context, attrs); 10 init(context, attrs, 0); 11 } 12 13 public ImageTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 14 super(context, attrs, defStyleAttr); 15 init(context, attrs, defStyleAttr); 16 } 17 18 private final void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 19 20 final ImageView imageView = new ImageView(context, attrs, defStyleAttr); 21 imageView.setScaleType(ImageView.ScaleType.FIT_XY); 22 final TextView textView = new TextView(context, attrs, defStyleAttr); 23 textView.setGravity(Gravity.CENTER); 24 25 26 // assetはcloseしない! 27 // asset.close(); 28 29 final TypedArray a = context.obtainStyledAttributes( 30 attrs, R.styleable.MyImageTextView); 31 32 final CharSequence text = a.getText(R.styleable.MyImageTextView_myText); 33 textView.setText(text); 34 35 final int resourceId = a.getResourceId(R.styleable.MyImageTextView_mySrc, 0); 36 imageView.setImageResource(resourceId); 37 38 textView.setTextAppearance(context, android.R.style.TextAppearance_Large); 39 40 a.recycle(); 41 42 final AssetManager asset = context.getAssets(); 43 textView.setTypeface(Typeface.createFromAsset(asset, "erasbd.ttf")); 44 45 addView(imageView); 46 addView(textView); 47 } 48 49 50 private final void init(Context context) { 51 52 final ImageView imageView = new ImageView(context); 53 imageView.setScaleType(ImageView.ScaleType.FIT_XY); 54 final TextView textView = new TextView(context); 55 textView.setEllipsize(TextUtils.TruncateAt.MARQUEE); 56 textView.setGravity(Gravity.CENTER); 57 58 final AssetManager asset = context.getAssets(); 59 textView.setTypeface(Typeface.createFromAsset(asset, "erasbd.ttf")); 60 61 addView(imageView); 62 addView(textView); 63 } 64 65}

投稿2015/10/28 14:08

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

includeを使わなければならない理由とは何でしょうか?
<com.example.mine.view.MyImageTextView></com.example.mine.view.MyImageTextView>上記のタグを直接使うことで解決できそうですが。

投稿2015/10/28 08:41

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2015/10/28 12:07 編集

<com.example.mine.view.MyImageTextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" app:mySrc="@drawable/example" app:myText="Example"> <ImageView android:id="@+id/myImageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:scaleType="fitXY" /> <TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textAppearance="?android:attr/textAppearanceLarge" /> </com.example.mine.view.MyImageTextView> として使うということでしょうか?これではソースコードの削減につながらないため,自分の目標の動作ではありません.すみません.
yona

2015/10/28 12:10

違います。 includeの箇所です。
退会済みユーザー

退会済みユーザー

2015/10/28 13:47 編集

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.mine.view.MyImageTextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:mySrc="@drawable/example" app:myText="Example"> </com.example.mine.view.MyImageTextView> </LinearLayout> でしょうか?これでは,MyImageTextViewの中に,ImageViewやTextViewがないため,上手くいきません.
yona

2015/10/28 14:10

現状のままではダメですね。 MyImageTextViewのコンストラクタに下記を追記してください。 View layout = LayoutInflater.from(context).inflate(R.layout.myview, this); 参考:http://zawapro.com/?p=1534
yona

2015/10/28 14:18

質問に対する根本的な対処ではない回答で解決済みになったことを残念に思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問