2016-12-10 2 views
3

CardViewのシャドウが非常に奇妙な動作をすることがわかりました。 私はちょうどアプリケーションを開発し始めます。ビジネスロジックなどはまったくありません。しかし、とにかく...Cardviewのシャドーが回転または壊れたカードビューの後に再描画される

私のフラグメントレイアウトにはいくつかのビューを追加するだけで、すべての画面回転後にカードビューの影が暗くなっていることがわかります。 5-6回転後、は既に完全に黒く見えます。私は、キャンバスのどこかに問題があるかもしれないと思うが、どこで、なぜ - 私は何もカスタマイズを開始しなかった。 誰かがすでにcardviewで同様の問題を解決し、その経験を共有できることを願っています。

ありがとうございました!ここで

スクリーンショット、コード、依存関係とxmlです:

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:iot="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/content_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="@dimen/common_gap" 
     iot:cardElevation="10dp" 
     iot:cardUseCompatPadding="true"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center_horizontal" 
       android:orientation="vertical"> 

       <DatePicker 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="@dimen/common_gap" 
        android:calendarViewShown="true" 
        android:spinnersShown="false" /> 

       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="?attr/selectableItemBackground" 
        android:paddingLeft="@dimen/common_gap" 
        android:paddingRight="@dimen/common_gap" 
        android:text="@string/choose_date_confirm_button" 
        android:textColor="@color/colorPrimary" /> 
      </LinearLayout> 

     </ScrollView> 

    </android.support.v7.widget.CardView> 
</LinearLayout> 

のJava(フラグメントのクラスでは何もない)

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.date_choose_fragment, container, false); 
     return rootView; 
    } 

依存関係

compile 'com.android.support:appcompat-v7:25.0.1' 
compile 'com.android.support:cardview-v7:25.0.1' 

Screenshots

答えて

4

これはCardViewまたはその影の描画を持つ任意のバグなどが原因ではありません。むしろ、これは透明な背景を持つ複数の背景の上に重ねられた複数のFragmentの結果である可能性があります。また、暗い影は影の半透明の相加効果です。

アクティブFragmentActivityの場合、オリエンテーションを変更するとインスタンスが自動的に再作成されます。たとえば、という動的インスタンスを無条件に追加する場合は、ActivityonCreate()メソッドでは、Activityの以前の状態から再作成されたインスタンスとともにそのインスタンスが追加されます。効果的には、デバイスを回転させるたびに、Fragmentをスタックに1つ追加すると、シャドウは少し暗くなります。

これはに固有の問題ではないことを示すために、単純なTextViewで示されます。例で

fragment.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:text="Hello world!" 
    android:textSize="60sp" 
    android:textStyle="bold" 
    android:textColor="#fffbc02d" 
    android:shadowColor="#70707070" 
    android:shadowDx="10" 
    android:shadowDy="10" 
    android:shadowRadius="10" /> 

ActivityonCreate()方法、我々は、上述のように、動的に、所定のレイアウトでFragmentを作成追加します。

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     getFragmentManager().beginTransaction() 
      .add(android.R.id.content, new MainFragment()).commit(); 
    } 

    public static class MainFragment extends Fragment { 
     public MainFragment() {} 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment, container, false); 
     } 
    } 
} 

このイメージは、デバイスを回転させた後の連続するキャプチャを各ステップごとに2回示しています。

screenshots

あなたはFragmentonCreateView()方法でログプリントを入れていた場合は、追加のインスタンスは、各回転時に作成されていることがわかります。このシーケンスの最後には、Fragmentが11個あります。

これを防止する1つの方法は、FragmentがすでにFragmentManagerに取り付けられているかどうかを確認してから、新しいものを作成して追加することです。FragmentTransactionにタグを追加することで可能です。たとえば:あなたは動的に起動後Fragmentを取引する必要がない場合

if (getFragmentManager().findFragmentByTag("main") == null) { 
    getFragmentManager().beginTransaction() 
     .add(android.R.id.content, new MainFragment(), "main").commit(); 
} 

あるいは、あなたの代わりに静的なレイアウトで定義することができ、そしてFragmentManagerは、チェックとトランザクション自体を処理します。

<fragment 
    android:id="@+id/main_fragment" 
    android:name="com.mycompany.myapp.MainFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
+1

あなたは絶対に正しいです!私は断片を作りました、私はチェックすることを忘れて、現在の断片は既に添付されています。詳細な回答をいただきありがとうございます。とても良い説明です – dosssik

関連する問題