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

Kotlinを使用してAndroidで実行時にTextViewのスタイルを変更するにはどうすればよいですか?


この例は、Kotlinを使用してAndroidで実行時にTextViewのスタイルを変更する方法を示しています。

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

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

<?xml version="1.0" encoding="utf-8"?>
<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"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="70dp"
      android:background="#008080"
      android:padding="5dp"
      android:text="TutorialsPoint"
      android:textColor="#fff"
      android:textSize="24sp"
      android:textStyle="bold" />
   <TextView
      android:id="@+id/textView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="Have a nice day!"
      android:textColor="@android:color/background_dark"
      android:textSize="36sp"
      android:textStyle="bold" />
</RelativeLayout>

ステップ3 − res / values / styles.xml

に次のコードを追加します
<resources>
   <style name="boldText">
      <item name="android:textStyle">bold|italic</item>
      <item name="android:textColor">#FFFFFF</item>
   </style>
   <style name="normalText">
      <item name="android:textStyle">normal</item>
      <item name="android:textColor">#C0C0C0</item>
   </style>
</resources>

ステップ4 −res/values/colors.xmlに次のコードを追加します

<resources>
   <color name="highlightedTextViewColor">@android:color/holo_green_light</color>
   <color name="normalTextViewColor">@android:color/holo_red_dark</color>
</resources>

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

に追加します
import android.os.Build
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
   lateinit var textView: TextView
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      textView = findViewById(R.id.textView)
      textView.setOnClickListener {
         if (Build.VERSION.SDK_INT < 23) {
            textView.setTextAppearance(applicationContext, R.style.boldText)
         }
         else {
            textView.setTextAppearance(R.style.boldText)
         }
         textView.setBackgroundResource(R.color.highlightedTextViewColor)
      }
   }
}

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="com.example.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で実行時にTextViewのスタイルを変更するにはどうすればよいですか? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します。

Kotlinを使用してAndroidで実行時にTextViewのスタイルを変更するにはどうすればよいですか?

Kotlinを使用してAndroidで実行時にTextViewのスタイルを変更するにはどうすればよいですか?


  1. Androidで実行時にtextViewスタイルを変更するにはどうすればよいですか?

    この例は、Androidで実行時にtextViewスタイルを変更する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 ステップ3 – res / values / styles.xmlに次のコードを追加します bold | italic #FFFFFF normal #C0C0C0

  2. Kotlinを使用してAndroidエミュレータのIPアドレスを取得するにはどうすればよいですか?

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