2015-10-21 4 views
4

Я content_main компоновкиКак открыть представление внутри прилежащим макета с использованием связывания данных

<?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=".MainActivity" 
 
    tools:showIn="@layout/activity_main"> 
 
    <TextView 
 
     android:id="@+id/textView" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" /> 
 
</RelativeLayout>

и activity_main

<?xml version="1.0" encoding="utf-8"?> 
 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
 
<android.support.design.widget.CoordinatorLayout 
 
    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=".MainActivity"> 
 

 
    <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_main" 
 
     android:id="@+id/content" 
 
     app:foo="@{1}"> 
 
    </include> 
 

 
    <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> 
 
</layout>

Сейчас в JavaУ меня есть

ActivityMainBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_main); 

Теперь я хочу, чтобы получить доступ к TextView внутри content_main в

binding.content.textView 

Я попробовал то, content_main в макет тега, но это не сработало. Я также следил за этим link, но он не работал

Как я могу это сделать?

+0

В ссылке, которую вы упомянули, есть упоминание о том, что с версией 1.0-rc4 она должна работать без передачи поддельных переменных, но в обоих файлах должен быть тег root . Я не вижу этот тег в макете content_main.xml. –

+0

Спасибо @maciej, что вы правы. У меня был тег макета в исходном коде, но я решил проблему, перестроив проект – krishna

ответ

0

Я решил проблему путем восстановления проекта, а также вы должны иметь тег макета в content_main XML слишком

0

Проверить это

hello_world.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

     <TextView 
       android:id="@+id/hello" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
     <include 
       android:id="@+id/included" 
       layout="@layout/included_layout"/> 
    </LinearLayout> 
</layout> 

included_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/world"/> 
</layout> 

В вашем Java файл

HelloWorldBinding binding = 
    HelloWorldBinding.inflate(getLayoutInflater()); 
binding.hello.setText(“Hello”); 
binding.included.world.setText(“World”); 

Шаблон для включаемых файлов по той же схеме, что и для Просмотры: идентификатор тега используется в качестве имени поля в классе. Включенный макет сгенерировал собственный класс со своими полями для представлений в его макете.

Простые, как вы видите. Source

Смежные вопросы