Skip to content

Instantly share code, notes, and snippets.

@yanzm
Created October 26, 2018 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanzm/cbbc26cdc8cedbfbb6dcb8c7c2a36e31 to your computer and use it in GitHub Desktop.
Save yanzm/cbbc26cdc8cedbfbb6dcb8c7c2a36e31 to your computer and use it in GitHub Desktop.
setCompoundDrawablesWithIntrinsicBounds と setCompoundDrawablesRelativeWithIntrinsicBounds で view.measure() の結果が違う
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)
}
}
}
<?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>
@yanzm
Copy link
Author

yanzm commented Oct 26, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment