Created
October 26, 2018 03:20
-
-
Save yanzm/cbbc26cdc8cedbfbb6dcb8c7c2a36e31 to your computer and use it in GitHub Desktop.
setCompoundDrawablesWithIntrinsicBounds と setCompoundDrawablesRelativeWithIntrinsicBounds で view.measure() の結果が違う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val metrics = resources.displayMetrics | |
val widthMeasureSpec = | |
View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.AT_MOST) | |
val heightMeasureSpec = | |
View.MeasureSpec.makeMeasureSpec(metrics.heightPixels, View.MeasureSpec.AT_MOST) | |
TextView(this).apply { | |
text = "Hello Android" | |
setCompoundDrawablesWithIntrinsicBounds( | |
R.drawable.square, | |
0, | |
R.drawable.square, | |
0 | |
) | |
}.let { | |
it.measure(widthMeasureSpec, heightMeasureSpec) | |
println(it.measuredWidth) | |
println(it.measuredHeight) | |
} | |
TextView(this).apply { | |
text = "Hello Android" | |
setCompoundDrawablesWithIntrinsicBounds( | |
ContextCompat.getDrawable(context, R.drawable.square), | |
null, | |
ContextCompat.getDrawable(context, R.drawable.square), | |
null | |
) | |
}.let { | |
it.measure(widthMeasureSpec, heightMeasureSpec) | |
println(it.measuredWidth) | |
println(it.measuredHeight) | |
} | |
TextView(this).apply { | |
text = "Hello Android" | |
setCompoundDrawablesRelativeWithIntrinsicBounds( | |
R.drawable.square, | |
0, | |
R.drawable.square, | |
0 | |
) | |
}.let { | |
it.measure(widthMeasureSpec, heightMeasureSpec) | |
println(it.measuredWidth) | |
println(it.measuredHeight) | |
} | |
TextView(this).apply { | |
text = "Hello Android" | |
setCompoundDrawablesRelativeWithIntrinsicBounds( | |
ContextCompat.getDrawable(context, R.drawable.square), | |
null, | |
ContextCompat.getDrawable(context, R.drawable.square), | |
null | |
) | |
}.let { | |
it.measure(widthMeasureSpec, heightMeasureSpec) | |
println(it.measuredWidth) | |
println(it.measuredHeight) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | |
<solid android:color="#3333ff" /> | |
<size | |
android:width="24dp" | |
android:height="24dp" /> | |
</shape> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
10-26 12:18:48.133 9643-9643/? I/System.out: 349
63
10-26 12:18:48.137 9643-9643/? I/System.out: 349
63
10-26 12:18:48.140 9643-9643/? I/System.out: 223
51
10-26 12:18:48.143 9643-9643/? I/System.out: 223
51