Hướng dẫn này minh họa cách đặt layout weight (trọng số bố cục) của TextView theo chương trình trong Android.
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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" />
</LinearLayout>
Bước 3: Viết code MainActivity.kt
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
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 button: Button = findViewById(R.id.button)
val layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
weight = 1.0f // Đặt weight = 1
}
button.layoutParams = layoutParams
}
}
Bước 4: Cấu hình 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>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Ban đầu Button có width="wrap_content". Nhấn Button "Hello World" sẽ set weight=1, khiến Button mở rộng chiếm toàn bộ chiều rộng còn lại của LinearLayout cha.
Giải thích layout_weight
layout_weightchỉ hoạt động trong LinearLayout (cha)- Weight = tỷ lệ không gian chia sẻ: weight=1 chiếm 1 phần, weight=2 chiếm 2 phần...
- Khi set weight > 0, nên set width/height =
0dpđể weight hoạt động chính xác
Ví dụ đầy đủ: Chia đều 3 TextView
val params = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT).apply {
weight = 1f
}
textView1.layoutParams = params
textView2.layoutParams = params
textView3.layoutParams = params
Lưu ý quan trọng
Code gốc có lỗi: Sử dụngRelativeLayout.LayoutParamscho Button trong LinearLayout cha. Phải dùngLinearLayout.LayoutParamsmới hỗ trợweight. Code đã được sửa trong ví dụ trên.