• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android Include的使用,获取include 里面的控件

武飞扬头像
(︶-︶メ)
帮助2

include 就是在一个布局中引入另一个布局,include 可以使相同的页面就写一次,提高了共同布局的复用性。

1.先定义一个共用的布局

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:orientation="vertical"
  4.  
    android:layout_width="match_parent"
  5.  
    android:layout_height="wrap_content"
  6.  
    android:id="@ id/include_layout"
  7.  
    android:clickable="true"
  8.  
    android:focusable="true">
  9.  
    <TextView
  10.  
    android:id="@ id/text_view"
  11.  
    android:layout_width="match_parent"
  12.  
    android:layout_height="wrap_content"
  13.  
    android:text="因为在一千年以后,世界早已没有我"
  14.  
    android:layout_margin="20dp"
  15.  
    android:textSize="20sp"
  16.  
    android:clickable="false"
  17.  
    android:focusable="false"
  18.  
    android:gravity="center"
  19.  
    android:background="@color/purple_200"/>
  20.  
    </LinearLayout>
学新通

2.使用include在布局中引用上面定义的布局

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    xmlns:app="http://schemas.android.com/apk/res-auto"
  4.  
    xmlns:tools="http://schemas.android.com/tools"
  5.  
    android:layout_width="match_parent"
  6.  
    android:layout_height="match_parent"
  7.  
    android:orientation="vertical"
  8.  
    tools:context=".MainActivity">
  9.  
     
  10.  
    <include layout="@layout/include_layout"
  11.  
    android:id="@ id/include1"/>
  12.  
    <include layout="@layout/include_layout"
  13.  
    android:id="@ id/include2"/>
  14.  
     
  15.  
    </LinearLayout>
学新通

引用两次一样的布局,需要添加id才可以分别修改它们的属性

3.在代码中尝试改变include布局的属性

  1.  
    public class MainActivity extends AppCompatActivity {
  2.  
    LinearLayout include1,include2;
  3.  
    TextView textView1,textView2;
  4.  
    @Override
  5.  
    protected void onCreate(Bundle savedInstanceState) {
  6.  
    super.onCreate(savedInstanceState);
  7.  
    setContentView(R.layout.activity_main);
  8.  
    include1 = findViewById(R.id.include1);
  9.  
    include2 = findViewById(R.id.include2);
  10.  
    include1.setBackgroundColor(Color.YELLOW);//给include1布局设置背景色
  11.  
    include2.setBackgroundColor(Color.BLUE);//给include2布局设置背景色
  12.  
    textView1 = include1.findViewById(R.id.text_view);//初始化include1里的textview
  13.  
    textView2 = include2.findViewById(R.id.text_view);//初始化include2里的textview
  14.  
    textView1.setBackgroundColor(Color.GREEN);//设置include1里textview的背景色
  15.  
    textView1.setText("心跳乱了节奏");//设置include1里textview的文字
  16.  
    textView2.setText("静止了,所有的花开");//设置include2里textview的文字
  17.  
     
  18.  
     
  19.  
    }
  20.  
    }
学新通

获取include里面的控件时注意,需要使用

textView1 = include1.findViewById(R.id.text_view);
textView2 = include2.findViewById(R.id.text_view);

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbjake
系列文章
更多 icon
同类精品
更多 icon
继续加载