我想在我的活动setContentView()布局上设置背景图像,并在我的片段视图下方始终显示相同的图像。我尝试在我的根布局上设置背景图像,并在子布局上将背景设置为透明,但它没有按预期工作。
聚苯乙烯。对于代码/布局转储感到抱歉,我宁愿把所有东西放在这里,然后留下可能导致问题的东西。
Picasso Init in activity onCreate:
Picasso.Builder b = new Picasso.Builder(getApplicationContext());
if (BuildConfig.DEBUG) {
b.loggingEnabled(true);
b.indicatorsEnabled(true);
}
b.memoryCache(new LruCache(getApplicationContext()));
Picasso.setSingletonInstance(b.build());
数据绑定代码:
private static Target t;
@BindingAdapter("android:backgroundUrl")
public static void setBackgroundUrl(final ViewGroup view,final String url) {
if(Strings.isNullOrEmpty(url)){
return;
}
t = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
view.setBackground(new BitmapDrawable(view.getResources(),bitmap));
Log.e("Picasso","onBitmapLoaded: " + from.name());
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e("BindingTools.setBackUrl","Failed to load: " + url);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
//do nothing
}
};
Picasso.with(view.getContext()).load(url).into(t);
}
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data class=".RootBinding">
<variable name="backgroundUrl" type="String"/>
</data>
<FrameLayout
android:backgroundUrl="@{backgroundUrl}"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType = "centerCrop">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/Transparent">
</FrameLayout>
</FrameLayout>
</layout>
示例片段布局:
<layout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Fragments.AccountFragment">
<data class=".AccountBinding">
<import type="android.view.View" />
<variable name="user" type="packageName.ViewModels.AccountViewModel"/>
<variable name="loginBtnEvent" type="View.OnClickListener"/>
<variable name="usernameError" type="String"/>
<variable name="passwordError" type="String"/>
<variable name="backgroundUrl" type="String"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/Transparent">
<android.support.v7.widget.CardView
android:layout_marginTop="16dp"
android:layout_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp">
<android.support.v7.widget.LinearLayoutCompat
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:elevation="0dp">
<android.support.v7.widget.LinearLayoutCompat
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:title="@string/app_name"
app:subtitle="@{!user.IsReAuth ? @string/login : @string/login_message_return, default=@string/login}"
app:subtitleTextColor="@color/cardview_light_background"
app:titleTextColor="@color/cardview_light_background">
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_gravity="end"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_margin="2dp"
app:civ_border_color="@color/colorPrimaryDark"
app:civ_border_width="4dp"
android:src="@{user.ImageUrl, default=@drawable/user}"
android:tint="@color/cardview_light_background"
/>
</android.support.v7.widget.Toolbar>
</android.support.v7.widget.LinearLayoutCompat>
<ProgressBar
android:visibility="@{user.IsLoadingAccount ? View.VISIBLE : View.INVISIBLE}"
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginBottom="-8dp"
android:layout_marginTop="-4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorAccent"
android:indeterminate="true"
android:indeterminateBehavior="cycle"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
android:errorText="@{usernameError}"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingStart="8dp"
android:paddingLeft="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:hint="@string/username"
android:text="@={user.UserName}"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
android:errorText="@{passwordError}"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingStart="8dp"
android:paddingLeft="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/password"
android:text="@={user.Password}"/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.ButtonBarLayout
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{!user.IsReAuth ? View.GONE : View.VISIBLE}"
android:text="@string/removeAccount"
style="@style/Base.Widget.AppCompat.Button.Borderless"/>
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:onClick="@{loginBtnEvent}"
style="@style/Base.Widget.AppCompat.Button.Borderless"/>
</android.support.v7.widget.ButtonBarLayout>
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.CardView>
</FrameLayout>
</layout>
设置片段的代码:(编辑:问题是此函数的第一行)
private void setRootFragment(Fragment f, boolean addToStack) {
rootBinding = (RootBinding) DataBindingUtil.setContentView(this, R.layout.root);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, f);
if(addToStack){
transaction.addToBackStack(null);
}else{
transaction.disallowAddToBackStack();
}
transaction.commit();
}
Strings.xml Trasparent值:
<color name="Transparent">#00000000</color>
答案 0 :(得分:0)
问题是每次我想切换片段时,我都在重置数据绑定对象并使其视图膨胀。
设置片段的代码:
private void setRootFragment(Fragment f, boolean addToStack) {
//this line of code needs to be in oncreate and not called every time I switch fragments.
//rootBinding = (RootBinding) DataBindingUtil.setContentView(this, R.layout.root);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, f);
if(addToStack){
transaction.addToBackStack(null);
}else{
transaction.disallowAddToBackStack();
}
transaction.commit();
}