英文:
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
<?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>
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
<?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>
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:
- Make a new layout file (for e.g. template_imageview.xml)
<?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" />
Notice that I have changed one of your attrubutes from fill_parent to match_parent.
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论