include的简单使用

本文详细介绍了如何在Android应用程序中使用XML定义统一的按钮样式,并通过Java代码动态设置包含多个按钮的TableRow布局的内容。从styles.xml文件的样式设置到main.xml布局文件的include引用,再到Java代码中获取并操作布局元素,提供了全面的实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.事前准备

<!--在res/values/styles.xml中-->
<!--设置样式-->
<style name="RemoteButton">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_margin">3dp</item>
        <item name="android:textColor">@drawable/button_text_action</item>
        <item name="android:background">@drawable/button_shape_shadowed</item>
</style>
<!--res/layout/include_button-->
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        style="@style/RemoteButton"
        />
    <Button
        style="@style/RemoteButton"
        />
    <Button
        style="@style/RemoteButton"
        />
</TableRow>
<!--res/layout/main.xml  应用linclude-->
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:stretchColumns="*"
    android:id="@+id/fragment_remove_control_table">
    <include
        android:layout_weight="1"
        layout="@layout/include_fragment_remove_control"
        />
    <include
        android:layout_weight="1"
        layout="@layout/include_fragment_remove_control"/>
    <include
        android:layout_weight="1"
        layout="@layout/include_fragment_remove_control"/>
    <include
        android:layout_weight="1"
        layout="@layout/include_fragment_remove_control"/>
</TableLayout>

2.应用Java代码获取include内容

TableLayout tableLayout = (TableLayout)v.findViewById(R.id.fragment_remove_control_table);
//利用getChild()方法获取控件内部的控件
for (int i=2; i<tableLayout.getChildCount()-1; ++i){
            TableRow row = (TableRow)tableLayout.getChildAt(i);
            for (int j=0; j<row.getChildCount(); ++j){
                Button button = (Button)row.getChildAt(j);
                button.setText(String.valueOf((i-2)*3+j+1));
                button.setOnClickListener(numberOnClick);
            }
}

 

转载于:https://www.cnblogs.com/rookiechen/p/5253935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值