我是Android数据绑定概念的新手,我已经从this博客阅读了它并且它正在使用他们提供的简单示例正常工作,我已经采取了空白活动,因此会有两个文件{{ 1}}和activity_main.xml
这是content_main.xml
代码
activity_main.xml
正如我所建议的那样,我已将父作为布局,并且从java方面我将按以下方式访问它
<android.support.design.widget.CoordinatorLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.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.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/stat_notify_sync_noanim" />
</android.support.design.widget.CoordinatorLayout>
</layout>
这没关系,我可以访问public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(activityMainBinding.toolbar);
//Floating Action Button
activityMainBinding.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
下的所有观看次数,但是为了访问activity_main.xml
内的内容,我已经在content_main.xml
MainActivity.java
问题是它无法更改ContentMainBinding contentMainBinding = DataBindingUtil.inflate(inflater,R.layout.content_main,clActivityMain,true);
contentMainBinding.tvHello.setText("yippee"); <br/>
EX:content_main.xml
中xml端提供的默认文本值,并显示两个值(在xml布局中提供+我从中设置的java方面)我已经在开发人员文档中阅读了它,但它与我读过的这篇博客有很大的不同。有没有正确的方法呢?作为开发人员文档或在this Blog?
答案 0 :(得分:3)
您可以查看:
https://medium.com/google-developers/android-data-binding-that-include-thing-1c8791dd6038#.p7nhkxhdt
您不需要对包含的布局进行充气,但如果您想要访问所包含的布局绑定,则应该为其添加ID:
public class EzFullScreenAds {
InterstitialAd mInterstitialAd;
Context context;
public EzFullScreenAds(Context context) { //constructor
this.context = context;
mInterstitialAd = new InterstitialAd(context);
mInterstitialAd.setAdUnitId(context.getString(R.string.adv_fullscreen));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if (mInterstitialAd.isLoaded()) mInterstitialAd.show();
}
});
}
}
现在,您可以访问以下内容:
new EzFullScreenAds(MainActivity.this);