Android
 Computer >> コンピューター >  >> プログラミング >> Android

Kotlinを使用してAndroidアプリでスピナーのテキストサイズとテキストの色を変更するにはどうすればよいですか?


この例は、Kotlinを使用してAndroidアプリでスピナーのテキストサイズとテキストの色を変更する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   tools:context=".MainActivity">  
   <Spinner
      android:id="@+id/spinner"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</RelativeLayout>

ステップ3 −次のコードをsrc / MainActivity.kt

に追加します
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
   lateinit var spinner: Spinner
      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      spinner = findViewById(R.id.spinner)
      val superHero = arrayOf<String?>("Batman", "SuperMan", "Flash", "AquaMan", "Shazam")
      val arrayAdapter: ArrayAdapter<Any?> = ArrayAdapter<Any?>(this, R.layout.spinner_list, superHero)
      arrayAdapter.setDropDownViewResource(R.layout.spinner_list)
      spinner.adapter = arrayAdapter
   }
}
>

ステップ4 −レイアウトリソースファイルを作成し、spinner_list.xmlに次のコードを追加します

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="10dp"
   android:textColor="@android:color/holo_red_light"
   android:textSize="16sp"
   android:textStyle="bold|italic" />

ステップ5 −次のコードをandroidManifest.xmlに追加します

<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]アイコンをクリックします ツールバーからKotlinを使用してAndroidアプリでスピナーのテキストサイズとテキストの色を変更するにはどうすればよいですか? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します。

Kotlinを使用してAndroidアプリでスピナーのテキストサイズとテキストの色を変更するにはどうすればよいですか?



  1. Android ListViewの色とフォントを変更するにはどうすればよいですか?

    この例は、AndroidListViewの色とフォントを変更する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http:

  2. アプリのAndroidに合わせてステータスバーの色を変更するにはどうすればよいですか?

    この例は、アプリのAndroidに合わせてステータスバーの色を変更する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="ht