【问题标题】:How to add an ImageView to an empty layout using java in android如何在android中使用java将ImageView添加到空布局
【发布时间】:2014-10-13 19:13:16
【问题描述】:

对于上述问题,我也在 stackoverflow 中找到了很多网站和很多主题。但我无法找到解决方案。所以请帮我解决这个问题。我正在尝试将 ImageView 添加到具有 RelativeLayout 的空布局,因为它是使用 JAVA 的父布局。请给我一个解决方案。在此先感谢

【问题讨论】:

标签: java android android-layout android-imageview


【解决方案1】:
  1. 为您的布局设置一个 ID。
  2. 使用findViewById 获取您的布局
  3. 创建一个新的ImageView
  4. ImageView 添加到您的布局中

例子:

LinearLayout layout = (LinearLayout) findViewById(R.id.your_layout_id);
ImageView imageView = new ImageView(this);
layout.addView(imageView);

注意:我使用了LinearLayout,因为你只说父布局是RelativeLayout,但忘了说你的空布局的类型。

【讨论】:

  • 它适用于线性布局。但它不适用于RelativeLayout
  • @c0deBeastx 你的意思是你的RelativeLayout 里面有一个RelativeLayout
  • 不,只有一个 RelativeLayout 。这是在Layout文件夹中创建xml文件时默认创建的
【解决方案2】:

将 imageView 添加到布局 XML 文件是最简单的,而不是从空行开始。因此,您的布局 XML 文件可能类似于:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".[YOURACTIVITYNAME">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"/>

</RelativeLayout>

【讨论】:

    【解决方案3】:

    要从 Java 中执行此操作,您可以在 Activity 中的几乎任何位置输入这样的代码。例如,在 onCreate 方法中,在您对布局进行膨胀之后。

    ViewGroup yourEmptyLayout = (ViewGroup) findViewById(R.id.whatever_your_layout_ID_is);
    
    ImageView myImageView = new ImageView(this);
    
    // Add the new ImageView to the layout
    yourEmptyLayout.addView(myImageView);
    
    
    // You should also give your ImageView a picture to draw.
    myImageView.setImageBitmap(bm);
    
    // or
    // myImageView.setImageDrawable(drawable);
    

    【讨论】:

      【解决方案4】:

      activity_main.xml

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" 
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/relativeLayout"
          tools:context=".MainActivity"/>
      

      MainActivity.java

      public class MainActivity extends Activity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
      
              ImageView imageView = new ImageView(MainActivity.this);
              imageView.setImageResource(R.mipmap.ic_launcher);
      
              RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
                              ViewGroup.LayoutParams.WRAP_CONTENT);
               params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
              relativeLayout.addView(imageView, params);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多