Ich möchte ein Hintergrundbild auf meine Aktivitäten SetContentView() Layout festlegen, und haben das gleiche Bild konsequent unter meinen Fragment Ansichten sichtbar. Ich habe versucht, das Hintergrundbild auf meinem Root-Layout zu setzen und den Hintergrund für untergeordnete Layouts transparent zu machen, aber es funktioniert nicht wie erwartet.Machen Sie Aktivität FrameLayout Hintergrund sichtbar unter Fragment-Layout
Ps. Sorry für den Code/Layout-Dump, ich würde lieber alles hier setzen und dann etwas auslassen, das das Problem verursachen könnte.
Picasso Init in onCreate Aktivität:
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());
Datenbindung Code:
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);
}
Aktivität Layout:
<?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>
Beispiel Fragment 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, [email protected]/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, [email protected]/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>
Code, das Fragment enthält: (Edit: Das Problem war die erste Zeile dieser Funktion)
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 Wert:
<color name="Transparent">#00000000</color>
Ziemlich dumm Fehler ..... – Theyouthis