java - Android Studio 中的 ImageButton 问题

标签 java android android-studio

我尝试在 Activity 中创建两个 ImageButton,但我只能看到其中一个。有人可以帮助我吗?

activity_ristorante.xml :


  ?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"
 tools:context=".Ristoranti">

<ImageButton
    android:id="@+id/b2"
    style="@style/Widget.AppCompat.ImageButton"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginStart="232dp"
    android:layout_marginTop="50dp"
    android:layout_marginEnd="30dp"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 <ImageButton
    android:id="@+id/b1"
    style="@style/Widget.AppCompat.ImageButton"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="50dp"
    android:visibility="visible"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

类(class):

public class Ristoranti extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    ImageButton b1 = findViewById(R.id.b1);
    ImageButton b2 = findViewById(R.id.b2);

    b1.setBackgroundResource(R.drawable.putia) ;
    b2.setBackgroundResource(R.drawable.pos);


    b1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Uri uri = Uri.parse("https://maps.app.goo.gl/FoRGA2CbJdrV5cfT8");
            Intent in = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(in);
        }
    });

有什么错误吗?即使在 android studio 中将其设置为“可见”,我也看不到该按钮,你能帮我吗?谢谢

最佳答案

只需将 setContentView(R.layout.activity_main); 更改为 setContentView(R.layout.activity_ristorante); 或从 activity_ristorante.xml 复制 XML 代码activity_main.xml

关于java - Android Studio 中的 ImageButton 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60012651/

相关文章:

java - 如何让 Android 按钮在点击时显示动画

android - 识别/proc/*/maps 中的代码段的好方法

java - android studio中的gradlew.bat无效目录

android - Android studio 中的 BoofCV gradle 构建错误

java - 如何获取自定义ListView中EditText的所有值

java - 如何在使用mapbox搜索时获取出发点和目的地点

java - 如何从数据库中插入图片到JTable

java - 在同一 JVM 中检测到多个 SparkContext

java - JBoss 中的自定义错误页面

android - 找不到Gradle DSL方法: 'multiDexEnabled';使用不包含方法的gradle