2016-08-13 10 views
0

含まれているレイアウトからTextViewにアクセスしようとしていますが、わかりません。 これは私のonCreate上のI setContentView(R.layout.activity_home);レイアウトです:別のxmlファイルから複数のレイアウトにアクセスする

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_home" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 

     app:menu="@menu/activity_home_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

あなたがこのレイアウトは、別のレイアウト、app_bar_home.xml含まれていることを見ることができます。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.branco.sauce.home"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 
</android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_home" 
     android:id="@+id/includeContentHome"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

今、この他のレイアウトは、私の最後のxmlファイルが含まを私が欲しいもの

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.branco.sauce.home" 
    tools:showIn="@layout/app_bar_home" 

    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ROLA 3" 
     android:textSize="100px" 
     android:id="@+id/txtRola3" 
     /> 
    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 

     /> 
</RelativeLayout> 

はidがあるのTextViewにアクセスすることである:1私はTextView、content_home.xmlを持っています以下の方法でtxtRola3:あなたが見ることができるように

@Override 
     protected void onPostExecute(Void result) { 

      final LayoutInflater inflateContentHome = getLayoutInflater(); 
      final View viewContentHome = inflateContentHome.inflate(R.layout.app_bar_home, null); 
      TextView txtTest = (TextView)viewContentHome.findViewById(R.id.includeContentHome).findViewById(R.id.txtRola3); 
      txtTest.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        System.out.println("You clicked on the textview"); 
       } 
      }); 
} 

、私はいくつかのレイアウトを膨らませてみたが、私はどのようにこの作業を行うには考えています。つまり、TextViewとそのテキストが表示されますが、listenerは機能しません。私はこれを実際にやっているので、私はListView(今はデータを入力できません)を学び、アクセスすることができます。私はこの種の質問が以前に答えられたことは知っていますが、3つのレイアウトと1つのレイアウトが含まれているので、他の人の回答には幸運がありません。

EDIT:home.java:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     new RemoteDataTask().execute(); 
    } 
private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 

     protected void onPostExecute(Void result) { 
      final LayoutInflater inflateContentHome = getLayoutInflater(); 
      final View viewContentHome = inflateContentHome.inflate(R.layout.app_bar_home, null); 
      TextView txtTest = (TextView)viewContentHome.findViewById(R.id.includeContentHome).findViewById(R.id.txtRola3); 
      txtTest.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        System.out.println("You clicked on the textview"); 
       } 
      }); 
} 
} 

OBS:私のコードは、およそ800のラインを持っているので、私は最も重要なものをここで追加しました。

+0

あなたは、私が<レイアウト=「@レイアウト/ app_bar_home」が含まれる/> @KrishnaJ – KrishnaJ

+0

を追加するためにを使用することができます'onCreate'または' onCreateView'であなたのビューを見つける必要があるかもしれませんし、あなたのコードのどこでもそれを使うことができます。 –

+0

、あなたは何を意味するか理解していないあなたはBrunoBrandã[email protected] KrishnaJ

答えて

0

3つのレイアウトがすべて「接続されている」と仮定して、インフレータを作成する必要はありません。私がしたのはtextviewがメインレイアウト上にあるかのようにfindViewByIDでした。

protected void onPostExecute(Void result) { 
       TextView k = (TextView)findViewById(R.id.rola3); 
       k.setOnClickListeer... 
} 
関連する問題