弱弱的问,android studio demo新建2个button怎么只显示第二个?

烟火...... 2019-12-04 10:17:59
可以2个button 都显示吗?
-----------------
package com.example.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

---------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
tools:layout_editor_absoluteX="97dp"
tools:layout_editor_absoluteY="65dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
tools:layout_editor_absoluteX="252dp"
tools:layout_editor_absoluteY="58dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="Hello www.baidu.com"
android:textColor="#601CD0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
...全文
373 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小林子(yhl) 2019-12-05
  • 打赏
  • 举报
回复
ConstraintLayout这个布局是约束布局,是居中显示的,前面被后面覆盖了。你选择linearlayout试试就好!还有button的ID一样,你是新手的新手吗
烟火...... 2019-12-05
  • 打赏
  • 举报
回复
的确有点新,嘿嘿,谢谢两位了!
兰特斯 2019-12-05
  • 打赏
  • 举报
回复
约束布局,具体使用方式和相对布局差不多,你需要写第二个布局相对于第一个布局的位置,例如:app:layout_constraintTop_toBottomOf="@+id/xxxx",id你自己定吧,这个意思是当前布局相位于xxxx的下方。
另外,两个button命名不能一毛一样,再不济你写成button1,button2,你这样没法正确布局及触发点击事件。

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧