<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:gravity="top"
android:singleLine="true"
android:includeFontPadding="false"
android:lineSpacingExtra="0dp" />
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.82" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.11" />
<Spinner
android:id="@+id/Galactic_size"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<string-array name="Galactic_Size">
<item>Малая|ﺃ</item>
<item>Средняя|ﺃ</item>
<item>Большая|ﺃ</item>
</string-array>
StartGameButton.setOnClickListener {
setContentView(R.layout.offline_game)
//val spinner = findViewById<Spinner>(R.id.Galactic_size)
val adapter = ArrayAdapter.createFromResource(this, R.array.Galactic_Size, R.layout.myspiner)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
/*spinner*/Galactic_size.adapter = adapter
//Неудачная попытка№ 1
//Galactic_size.getChildAt(0).findViewById<TextView>(text1.id).setTextSize(TypedValue.COMPLEX_UNIT_PX, 50f)
//Неудачная попытка№ 2
//Galactic_size.findViewById<TextView>(text1.id).setTextSize(TypedValue.COMPLEX_UNIT_PX, 50f)
//Неудачная попытка№ 3
//(Galactic_size.adapter.getItem(0) as TextView).setTextSize(TypedValue.COMPLEX_UNIT_PX, 50f)
//Работающий но часто вызываемый лишний раз метод
val OnCatSpinnerCL = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
val TAG = "myLogs"
Log.d(TAG, "Начало")
var finalTextSize=14f
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
paint.style = Paint.Style.FILL
//paint.color = color
//paint.textAlign = Paint.Align.CENTER
paint.textSize = 100f //размер шрифта в **пикселях**
val bounds = Rect()
var textheight = 0
var textwidth = 0
for (i in resources.getStringArray(R.array.Galactic_Size).indices) {
val text = resources.getStringArray(R.array.Galactic_Size)[i]
paint.getTextBounds(text, 0, text.length, bounds)
Log.d(TAG, bounds.height().toString() + " : " + bounds.width().toString())
if (textheight < bounds.height()) textheight = bounds.height()//высота
if (textwidth < bounds.width()) textwidth = bounds.width()//ширина
}
Log.d(TAG, textheight.toString() + " : " + textwidth.toString())
Log.d(TAG, Galactic_size.measuredHeight.toString() + " : " + (parent.getChildAt(0) as TextView).measuredHeight.toString())
if (Galactic_size.measuredHeight.toFloat() / (textheight.toFloat() / 95f) < Galactic_size.measuredWidth.toFloat() / (textwidth.toFloat() / 95f)){
finalTextSize=Galactic_size.measuredHeight.toFloat() / (textheight.toFloat() / 95f)
}else{
finalTextSize=Galactic_size.measuredWidth.toFloat() / (textwidth.toFloat() / 95f)
}
Log.d(TAG, finalTextSize.toString())
(parent.getChildAt(0) as TextView).setTextColor(Color.BLUE)
(parent.getChildAt(0) as TextView).setTextSize(TypedValue.COMPLEX_UNIT_PX, finalTextSize)
}
override fun onNothingSelected(parent: AdapterView<*>) {
}
}
Galactic_size.onItemSelectedListener = OnCatSpinnerCL
}