KotlinでAndroidのTextViewにレイアウトの重み(layout_weight)をプログラムから設定する方法
はじめに
このチュートリアルでは、Kotlinを使用してAndroidアプリのTextViewにレイアウトの重み(layout_weight)をプログラムから動的に設定する方法を解説します。XMLレイアウトだけではなく、コード側からビューのサイズや比重を制御したい場合に役立つテクニックです。
手順1:新規プロジェクトを作成する
Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成してください。
手順2:activity_main.xml にレイアウトを定義する
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"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="expand"
android:text="Hello World"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
手順3:MainActivity.kt を実装する
src/MainActivity.kt に以下のコードを記述します。ボタンの LayoutParams をコードから生成し、動的に幅と高さを変更しています。
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
}
fun expand(view: View) {
val height = RelativeLayout.LayoutParams.WRAP_CONTENT
val width = 200
val layoutParams = RelativeLayout.LayoutParams(width, height)
val button: Button = findViewById(R.id.button)
button.layoutParams = layoutParams
}
}
なお、LinearLayoutの子ビューに対してレイアウトの重み(weight)を明示的に設定したい場合は、LinearLayout.LayoutParams の weight プロパティを使用します。
val textView = findViewById<TextView>(R.id.textView)
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
params.weight = 1f
textView.layoutParams = params
手順4:AndroidManifest.xml を確認する
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スマートフォンがPCに接続されているものとします。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーにある「Run」アイコン
をクリックしてください。ドロップダウンから接続したモバイルデバイスを選択すると、端末の画面に結果が表示されます。


-
AndroidでTextViewのレイアウトの重み(weight)をプログラム的に設定する方法
この記事では、AndroidアプリでTextViewのレイアウトパラメータ(レイアウトの重み)をプログラム的に設定する方法を、実際のサンプルコードとともに解説します。 手順1:新しいプロジェクトを作成する Android Studioを起動し、「File」→「New Project」を選択して、必要な項目を入力し新しいプロジェクトを作成します。 手順2:activity_main.xmlにコードを追加する res/layout/activity_main.xmlに以下のコードを追加します。 <RelativeLayout xmlns:android="https://s
-
【Android開発】TextViewのSpan(文字列の一部)に色を設定する方法を解説
この記事では、AndroidアプリでTextView内の特定の文字列(Span)に色を設定する方法を、実際のサンプルコードとともに解説します。SpannableStringとForegroundColorSpanを組み合わせれば、1つのTextViewの中でも部分的にテキストの色を変更できます。手順1:Android Studioで新規プロジェクトを作成Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目を入力して、新しいプロジェクトを作成してください。手順2:レイアウトファイル(activity_main.xml)を編集するres