【Android-布局复用】 多个界面复用一个布局文件(二)

多个界面复用一个布局界面 ,如何找到复用布局文件中的控件的id?

 

举个栗子:

1.  layout_common.xml  

复用的布局文件,如何找到button 的id?

<?xml version="1.0" encoding="utf-8"?>
<!-- 复用的布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/common_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1" />

</LinearLayout>

2.layout_main.xml

关键是给include的布局添加一个id

<?xml version="1.0" encoding="utf-8"?>
<!-- 主布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 在布局文件中引用复用的布局文件 -->

    <include
        android:id="@+id/common_layout"
        layout="@layout/layout_common" />

</LinearLayout>
 

3.MainActivity.java

然后就可以通过以下代码找到 复用布局文件控件的id了。

View view=findViewById(R.id.common_layout);
Button button=view.findViewById(R.id.common_button1);

 

转载于:https://www.cnblogs.com/Sukie-s-home/p/7527498.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值