【发布时间】:2020-04-28 04:25:17
【问题描述】:
我需要完成以下任务:
1) 四个按钮中的每一个都保存在自己的相对或框架内 布局
2) 这四个带有按钮的布局被添加到
另一个更大的布局通过include
标记
3)这个更大的布局是改变这些的ID和文本 按钮
到目前为止,我做了以下事情:
为了完成任务 #1,我创建了 res/layout/big_button_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:showIn="@layout/activity_main">
<my.planner.BigButton
android:id="@+id/nonameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
/>
</RelativeLayout>
为了完成任务 #2 和 #3,我在 res/layout/big_buttons_layout.xml
中创建了更大的布局<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<include
layout="@layout/big_button_layout"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title1"
/>
<include
layout="@layout/big_button_layout"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title2"
/>
<include
layout="@layout/big_button_layout"
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title3"
/>
<include
layout="@layout/big_button_layout"
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title4"
/>
我有两个问题:
1) 为什么按钮不可见?
2) 如何更改较大布局中按钮的 ID 和文本?
【问题讨论】:
-
您不能更改元素的 ID。您可以更改文本,但可以更改代码,而不是 XML。
-
作为可见性 - 在 layout_height 的
标签中使用 WRAP_CONTENT -
谢谢。那么在自己的布局中添加一个按钮不是一个好主意吗?
标签: android android-layout android-include