通过代码在Android Studio中添加一个ImageView。

huangapple go评论80阅读模式
英文:

Add an ImageView through code Android Studio

问题

以下是您提供的内容的翻译部分:

<?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>
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);
        ImageView imageView_ = findViewById(R.id.imageView);
        //我认为可以尝试类似以下方式:
        findViewById(R.id.container).add(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>

以上是您提供的内容的翻译部分。

英文:

I Have a problem, is that I can't find out how to add any component(in this case an imageview)

These are the xml code for the layout

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/Layout&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.main.Main&quot;&gt;
    &lt;LinearLayout
        android:id=&quot;@+id/container&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:orientation=&quot;horizontal&quot;
        tools:ignore=&quot;MissingConstraints&quot; /&gt;
    &lt;ImageView
        android:id=&quot;@+id/imageView&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;56dp&quot;
        android:layout_marginLeft=&quot;56dp&quot;
        android:layout_marginTop=&quot;40dp&quot;
        android:contentDescription=&quot;@string/Nada&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:srcCompat=&quot;@drawable/ic_launcher_foreground&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

And this is the code that I have tried so far to add an imageView

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);
         ImageView imageView_ = findViewById(R.id.imageView);
         //Im thinking something like this:
         findViewById(R.id.container).add(imageView_);
         //
    }
}

And lastly here is the one named AndroidManifest.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.serverexample&quot;&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.main.Main&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

This is just so stackoverflow let me post this edit becuause it says that it has so much code and not that much details
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

答案1

得分: 1

由于您提到您需要以编程方式填充ImageView,以下是您可以使用的方法:
1. 创建一个**新的布局文件**(例如template_imageview.xml)

请注意,我已将其中一个属性从**fill_parent**更改为**match_parent**。

2. 现在通过代码来填充和添加ImageView:

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);

    // 填充ImageView,将LinearLayout作为父项)
    ImageView imageView_ = (ImageView) getLayoutInflater().inflate(R.layout.template_imageview, containerLayout, false);

    // 将imageView添加到LinearLayout
    containerLayout.addView(imageView_);
}

}


您尝试以编程方式进行的方法不会起作用,因为ImageView已经有一个父布局(ConstraintLayout),当您尝试执行`findViewById(R.id.container).addView(imageView_);`行时,将会出现运行时异常。
英文:

Since you've mentioned you need an imageview populated programmatically, here's how you can approach it:

  1. Make a new layout file (for e.g. template_imageview.xml)
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;ImageView
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/imageView&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;56dp&quot;
    android:layout_marginLeft=&quot;56dp&quot;
    android:layout_marginTop=&quot;40dp&quot;
    android:contentDescription=&quot;@string/Nada&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    app:srcCompat=&quot;@drawable/ic_launcher_foreground&quot; /&gt;

Notice that I have changed one of your attrubutes from fill_parent to match_parent.

  1. Now inflate and add the imageview by code:
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);
    }
}

The way you are trying to do it programmatically won't work, as ImageView already has a parent layout (ConstraintLayout) and you will get a Runtime Exception when you try to do the findViewById(R.id.container).addView(imageView_); line.

答案2

得分: 0

你的活动类的名称是什么?

在约束布局(Constraint Layout)和 AndroidMainfest.xml 中的 tools:context=".main.Main" 属性是将布局与你的活动类连接起来的地方。

如果上述步骤已经处理好了,那么 ImageView imageView_ = findViewById(R.id.imageView); 应该能让你访问到你的 imageView。

英文:

What is the name of your activity class?

tools:context=".main.Main" attribute in the Constraint Layout and AndroidMainfest.xml is where you will tie the Layout with your activity class.

If the above steps have been taken care of then ImageView imageView_ = findViewById(R.id.imageView); should give you access to your imageView.

huangapple
  • 本文由 发表于 2020年10月12日 07:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64310089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定