アプリケーションのメイン画面にカスタム「ビュー」ウィジェット (CustomController) があります。
ウィジェットのコンストラクターでは、次のコードを使用してウィジェットの xml ファイル内の画像にアクセスできます。
(注、img_cursor は「custom_controller.xml」の ImageView です)
public final class CustomController extends LinearLayout{
public CustomController(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view_controller = layoutInflater.inflate(R.layout.custom_controller,this);
img_cursor = (ImageView) view_dpad.findViewById(R.id.img_cursor);
...
ただし、メイン アクティビティの画面で TextView にアクセスしたいと考えています。カスタム コントローラー ウィジェットは、main.xml を介してメイン画面に表示されます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linlayRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.test.projectname.CustomController
android:id="@+id/dpad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="15px"
android:layout_marginLeft="15px" />
<Button
android:text="Connect"
android:id="@+id/btn_connection"
android:layout_height="wrap_content"
android:layout_width="100dp">
</Button>
<Button
android:text="Settings"
android:id="@+id/btn_settings"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</Button>
<Button
android:text="Debug x,y"
android:layout_width="wrap_content"
android:id="@+id/btn_debug_x_y"
android:layout_height="wrap_content">
</Button>
<TextView
android:id="@+id/txt_view_x"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X = 0">
</TextView>
<TextView
android:id="@+id/txt_view_y"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y = 0">
</TextView>
</LinearLayout>
CustomController クラスから main.xml の TextViews にアクセスするにはどうすればよいですか?
私は次のようなものを試しました:
public final class CustomController extends LinearLayout{
public CustomController(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main_controller = layoutInflater.inflate(R.layout.main,this);
textview = (TextView) view_dpad.findViewById(R.id.txt_view_x);
...
メイン画面のビューにアクセスできないようです。それは好きではありません:
View main_controller = layoutInflater.inflate(R.layout.main,this);
助けてくれてありがとう!