Android include 细节问题

20 篇文章 0 订阅

include标签可以很好的解决布局复用的问题, 用起来也很简单.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/head_layout" />

</RelativeLayout>

head_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/head_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:background="@color/colorAccent"
    android:text="Hello World!"
    tools:showIn="@layout/activity_main" />

就可以达到如图效果

这里写图片描述

接着看

include标签里面可以指定很多layout属性, 用起来有一些需要注意的地方.
这里写图片描述

修改activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/head_layout" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/head_text"
        android:background="@color/colorPrimary" />

</RelativeLayout>

可以发现, include标签里面的id可以直接在activity_main.xml中使用

现在给include标签加一个id

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/head"
        layout="@layout/head_layout" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/head"
        android:background="@color/colorPrimary" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/head"
        layout="@layout/head_layout" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/head"
        android:background="@color/colorPrimary" />

</RelativeLayout>

可以发现android:layout_below=”@+id/head”也生效了, 直接使用include里面的标签也可以的.

这里给include加上id后, 还要说一个东西放到后面说

现在我们在include里面加上一句android:layout_alignParentBottom=”true”

<include
        android:id="@+id/head"
        layout="@layout/head_layout"
        android:layout_alignParentBottom="true" />

结果发现Android studio报错了.

Layout parameter layout_alignParentBottom ignored unless both layout_width and layout_height are also specified on tag

大意是如果不指定layout_width and layout_height, layout_alignParentBottom 属性将被忽略

加上
android:layout_width=”match_parent”
android:layout_height=”wrap_content”

<include
        android:id="@+id/head"
        layout="@layout/head_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

这里写图片描述
可以发现加上android:layout_width android:layout_height
android:layout_alignParentBottom=”true”才会生效

如果在include 标签中覆盖被包含布局所指定的 android:layout_***
属性,同时指定 android:layout_width 和 android:layout_height才会生效

刚刚在include 里面 加上了 android:id=”@+id/head”
下面在MainActivity里面添加代码

public class MainActivity extends AppCompatActivity {
    private Button mHeadText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHeadText = (Button) findViewById(R.id.head_text);
        mHeadText.setBackgroundColor(Color.RED);
    }
}

发现抛出了一个异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setBackgroundColor(int)' on a null object reference

(~ o ~)~zZ

include 里面 没加 android:id=”@+id/head” 的时候上述代码是可以正常运行并生效的.

这个时候我们需要先得到布局文件填充的View, 在通过View.findViewById来找到head_text

public class MainActivity extends AppCompatActivity {
    private Button mHeadText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View view = getLayoutInflater().inflate(R.layout.head_layout, null);
        mHeadText = (Button) view.findViewById(R.id.head_text);
        mHeadText.setBackgroundColor(Color.RED);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值