Java 通过代码Android Studio添加ImageView

Java 通过代码Android Studio添加ImageView,java,android-studio,Java,Android Studio,我有一个问题,就是我找不到如何添加任何组件(在本例中是imageview) 这些是布局的xml代码 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ap

我有一个问题,就是我找不到如何添加任何组件(在本例中是imageview)

这些是布局的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:id="@+id/Layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.Main">
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:ignore="MissingConstraints" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="56dp"
        android:layout_marginLeft="56dp"
        android:layout_marginTop="40dp"
        android:contentDescription="@string/Nada"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_foreground" />
</androidx.constraintlayout.widget.ConstraintLayout>
最后是一个名为AndroidManifest.xml的文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.serverexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".main.Main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这就是stackoverflow,让我发布这个编辑,因为它说它有这么多代码,没有那么多细节
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

您的活动类名称是什么

tools:context=“.main.main”属性在约束布局中,AndroidMainfest.xml是将布局与活动类绑定的位置


如果已完成上述步骤,则ImageView ImageView=findViewById(R.id.ImageView);应该允许您访问imageView。

既然您提到您需要以编程方式填充imageView,下面是您的方法:

  • 创建一个新的布局文件(例如,template_imageview.xml)

  • 您尝试以编程方式执行此操作的方式将不起作用,因为ImageView已具有父布局(ConstraintLayout),并且当您尝试执行
    findViewById(R.id.container).addView(ImageView)时,将出现运行时异常行。

    main是存储类main的包。xml类名是activity_main.xml您可以共享您的activity_main.xml吗?我共享的xml代码是activity_main.xml代码该类下的类是主类。您可以共享您的AndroidManifest.xml而不是activity_main.xml吗?我已经添加了它不,我确实需要通过代码添加imageview,因为您喜欢按按钮召唤一个新的图像视图如果是这样,代码需要改变很多,我会在几个小时内更新我的答案。但是每次用户单击按钮时,您是否需要一个新的imageview?还是只有一个?每次都是新的。在template_imageview中,我需要放置什么?因为:ImageView ImageView=(ImageView)GetLayoutFlater().充气(R.layout.activity_main,containerLayout,false);,引发此异常:原因:java.lang.ClassCastException:androidx.constraintlayout.widget.constraintlayout无法强制转换为android.widget.ImageView.Do请正确检查我的代码,它不是R.layout.activity\u主充气方法,而是R.layout.template\u ImageView
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.serverexample">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".main.Main">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    <?xml version="1.0" encoding="utf-8"?>
    <ImageView
        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:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="56dp"
        android:layout_marginLeft="56dp"
        android:layout_marginTop="40dp"
        android:contentDescription="@string/Nada"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_foreground" />
    
    package com.example.servidorexample.main;
    
    import androidx.annotation.RequiresApi;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    
    public class Main extends AppCompatActivity implements Observer {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            LinearLayout containerLayout = findViewById(R.id.container);
    
            // Inflate the imageview, giving the linearlayout as the parent)
            ImageView imageView_ = (ImageView) getLayoutInflater().inflate(R.layout.template_imageview, containerLayout, false);
    
            // Add the imageView to the linearlayout
            containerLayout.addView(imageView);
        }
    }