20.TextView、Buttonなどの設定setCompoundDrawablesは無効です
1957 ワード
シーン
TextView上に画像が表示されるように設定すると表示されない現象が発生します.コードは次のとおりです.
private void clickShowOrHiddenFlightDetail(View view) {
Drawable rightDrawable = (mRvFlightSegment.getVisibility() == View.VISIBLE ) ? ContextCompat.getDrawable(this, R.drawable.ic_top_jiantou) : ContextCompat.getDrawable(this, R.drawable.ic_down_jiantou);
mTvFlightDetail.setCompoundDrawables(null, null, rightDrawable, null);
}
API解釈
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.
Drawableをワイドに設定すればよい
ソリューション1:
公式apiの解釈によるとdrawable.setBounds(Rect) ;
ソリューション2:
setCompoundDrawables(left,top,right,bottom)ではなく、setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom)を呼び出します.Drawableをtextの左、上、右、下に表示するように設定します.
private void clickShowOrHiddenFlightDetail(View view) {
Drawable rightDrawable = (mRvFlightSegment.getVisibility() == View.VISIBLE ) ? ContextCompat.getDrawable(this, R.drawable.ic_top_jiantou) : ContextCompat.getDrawable(this, R.drawable.ic_down_jiantou);
mTvFlightDetail.setCompoundDrawablesWithIntrinsicBounds(null, null, rightDrawable, null);
}
両者にはいくつかの違いがあります。
setCompoundDrawables(left,top,right,bottom)およびsetCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom):
setCompoundDrawablesが描いたdrawableの幅はdrawableである.setBound()は幅が広いので、The Drawables must already have had setBounds(Rect)called.使用前にDrawableを使用する必要がある.setBounds Drawableの長さと幅を設定します.
S t e C o m poundDrawablesWithIntrinsicBoundsは描いたdrawableの幅の高さがdrawableで固定された幅の高さなので、The Drawables'bounds will be set to their intrinsic boundsがあります.すなわち、getIntrinsicWidth()とgetIntrinsicHeight()で取得される.