0

アクティビティのsetContentView()レイアウトに背景イメージを設定し、同じイメージを常にフラグメントビューの下に表示したいと思います。私は自分のルートレイアウトで背景イメージを設定し、子レイアウトで背景を透明に設定しようとしましたが、期待通りに動作しません。アクティビティFrameLayoutの背景がフラグメントレイアウトの下に表示されるようにする

ps。申し訳ありませんが、コード/レイアウトのダンプは、私はむしろここにすべてを置いて、問題を引き起こす可能性のあるものを残しておきます。

ピカソ初期活性の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, [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> 

断片を設定するコード(編集:問題は、この関数の最初の行であった)

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

問題は、私はデータをリセットしたことでフラグメントを切り替えるたびにビューを膨張させます。フラグメントを設定します

コード:

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(); 
} 
+0

プリティダム間違い..... – Theyouthis

関連する問題