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

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

新規登録して質問してみよう
ただいま回答率
85.51%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Android

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

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

4397閲覧

【Xamarin】axmlで設定されている画像をC#側から変更したい

alimitsu

総合スコア7

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Android

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

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2016/04/17 05:46

###前提・実現したいこと
Table.axmlで設定されている"red.png"を"blue.png"へC#から変更したいです。

###発生している問題
エラーは起きませんが、画像が変更されないです。
また、同じような方法でテキストの変更も試してみましたが、変更されなかったです。

###該当のソースコード

C#

1private Context context = null; 2 3public void ShowDialog(Context ct) { 4 context = ct; 5 var dialog = new Dialog(context); 6 AttachImageToTable(); 7 dialog.SetContentView(LayoutInflater.From(context).Inflate(Resource.Layout.Table, null)); 8 dialog.SetCanceledOnTouchOutside(true); 9 dialog.Show(); 10} 11 12private void AttachImageToTable() { 13 View inf = LayoutInflater.From(context).Inflate(Resource.Layout.Table, null); 14 FrameLayout parent = inf.FindViewById<FrameLayout>(Resource.Id.c); 15 for (int i = 0; i < parent.ChildCount; i++) { 16 var child = parent.GetChildAt(i); 17 if (child is ImageView) { 18 (child as ImageView).SetImageResource(Resource.Drawable.blue); 19 } 20 } 21}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:layout_weight="1" 7 android:shrinkColumns="0,1,2" 8 android:id="@+id/tableLayout1"> 9 <TableRow 10 android:id="@+id/tableRow1" 11 android:orientation="horizontal" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content"> 14 <include 15 layout="@layout/TableItem" 16 android:id="@+id/a" /> 17 <include 18 layout="@layout/TableItem" 19 android:id="@+id/b" /> 20 <FrameLayout 21 android:layout_weight="1" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:id="@+id/c"> 25 <ImageView 26 android:src="@drawable/red" 27 android:adjustViewBounds="true" 28 android:scaleType="fitXY" 29 android:layout_width="fill_parent" 30 android:layout_height="fill_parent" /> 31 <TextView 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:layout_gravity="center|center_vertical" 35 android:clickable="false" 36 android:textSize="12sp" 37 android:text="test" 38 android:textColor="#ffffff" /> 39 </FrameLayout> 40 </TableRow> 41</TableLayout>

###試したこと
・AttachImageToTable()の呼ぶタイミングを変更してみましたが、変更されませんでした。
・ImageViewにIDを設定して直接変えようとしてみましたが、変更されませんでした。

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

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

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

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

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

guest

回答1

0

ベストアンサー

こんにちは。

ImageResourceの再設定を行ったViewを結局使ってないから表示されていないのだと思います。

C#

1private Context context = null; 2 3public void ShowDialog(Context ct) 4{ 5 context = ct; 6 var dialog = new Dialog(context); 7 8 //AttachImageToTable(); 9 //dialog.SetContentView(LayoutInflater.From(context).Inflate(Resource.Layout.Table, null)); 10 dialog.SetContentView(AttachImageToTable()); 11 12 dialog.SetCanceledOnTouchOutside(true); 13 dialog.Show(); 14} 15 16//private void AttachImageToTable() 17private View AttachImageToTable() //mod 18{ 19 View inf = LayoutInflater.From(context).Inflate(Resource.Layout.Table, null); 20 FrameLayout parent = inf.FindViewById<FrameLayout>(Resource.Id.c); 21 for(int i = 0; i < parent.ChildCount; i++) 22 { 23 var child = parent.GetChildAt(i); 24 if(child is ImageView) 25 { 26 (child as ImageView).SetImageResource(Resource.Drawable.blue); 27 } 28 } 29 return inf; //add 30}

投稿2016/04/17 06:08

Tak1wa

総合スコア4791

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

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

alimitsu

2016/04/17 06:20

迅速なご回答、ありがとうございます! 無事、変更されました! 直接Table.axmlを変更していると思い込んでいたのが盲点でした。 Table.axmlを改変したinfを適用する必要があったのですね。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問