发表于: 2019-01-13 23:29:46

0 907


今天完成的事情

onAttach(Context)在API23不响应解决方法

在API23以后已经将onAttach(Activity activity)弃用了,改用onAttach(Context context )但是context 的值会为null

 在fragment向activity传值时需要使用onAttach方法,由于onAttach(activity,Activity):void已被弃用,只能用onAttach(contex,Contex):void,但是在API<23时并不会去调用此方法,即会导致程序崩溃(此为google一个bug)。

  解决方法有3个:

1.Using getSupportFragmentManager() will force you to use a Fragment from the support lib. So the first solution is to replace all fragments with the fragment from support lib and using getSupportFragmentManager().

  使用 getSupportFragmentManager() 会强行使用该 support lib 里的Fragment, 所以通过使用getSupportFragmentManager()把所有原Fragment都替换成support lib 里的Fragment.

2.Solution that I've already implemented is to handle 2 possibilities

@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
   super.onAttach(activity);
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
       if (activity instanceof ReturnDataListener) {
           returnDataListener = (ReturnDataListener) activity;
       } else {
           throw new IllegalArgumentException("activity must implements FragmentInteraction");
       }
   }

}

@TargetApi(23)
@Override
public void onAttach(Context context) {
   super.onAttach(context);
   if (context instanceof ReturnDataListener) {
       returnDataListener = (ReturnDataListener) context;
   } else {
       throw new IllegalArgumentException("activity must implements FragmentInteraction");
   }
}


控件保持固定宽高比

在开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但是组件本身大小却不定。尤其在android屏幕碎片化的情况下,很多时候我们需要让一个组件宽度与屏幕宽度一致,这样就无法确定宽度

1.自定义View

重写onMeasure或onLayout等相关方法,通过预定的比例计算宽高。

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   int width = MeasureSpec.getSize(widthMeasureSpec);
   if (mRatio != 0) {
       float height = width / mRatio;
       heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY);
   }
   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

2.ImageView的adjustViewBounds

为ImageView设置adjustViewBounds,如下:

android:adjustViewBounds="true"

这个方法只能用于ImageView


3.百分比布局

<android.support.percent.PercentFrameLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
   <ImageView
       android:layout_width="0dp"
       android:layout_height="0dp"
       android:scaleType="fitXY"
       app:layout_widthPercent="100%"
       app:layout_aspectRatio="@fraction/circle_article_aspectRatio"
       />
</android.support.percent.PercentFrameLayout>

在res/values下新建一个fraction.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="circle_article_aspectRatio" type="fraction">133%</item>
</resources>

这样就实现了宽高4:3的比例


4.ConstraintLayout的DimensionRatio

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:src="@mipmap/bb"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintDimensionRatio="4:3"
   />
</android.support.constraint.ConstraintLayout>

可能产生的几个情况如下:

1、如果组件宽高都设置0dp,组件宽高按比例,且只受父view的约束。如图

2、如果其中一个设置成了wrap_content或match_parent,比如说宽度,那么宽度就会是图片的真实宽度和父view的限制宽度的较小值,而高度会根据宽度和比例计算出来。

这时如果图片较小,就不会撑满父View。如图

3、如果宽高都设置成了wrap_content或match_parent,则比例失效。如图:

注意:

1、当宽高都设置为0dp时,app:layout_constraintLeft_toLeftOf="parent"和app:layout_constraintRight_toRightOf="parent"不能省略,否则组件大小为0。

2、当宽高其中一个或都设置为wrap_content时,如果去掉app:layout_constraintLeft_toLeftOf="parent"和app:layout_constraintRight_toRightOf="parent"则比例失效。


明天的计划

完成视频列表、文章列表以及文章详情页


收获

保持控件固定宽高比的方法



返回列表 返回列表
评论

    分享到