Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何以编程方式放置图像以匹配所有布局?_Android_Layout_Imageview - Fatal编程技术网

Android 如何以编程方式放置图像以匹配所有布局?

Android 如何以编程方式放置图像以匹配所有布局?,android,layout,imageview,Android,Layout,Imageview,我想在所有布局上匹配一个图像 我在这里看到了一幅图像: 我什么都试过了,这是密码 <LinearLayout android:id="@+id/mid_linear_layout" android:layout_width="match_parent" android:layout_height="match_parent" ...> <TableL

我想在所有布局上匹配一个图像

我在这里看到了一幅图像:

我什么都试过了,这是密码

    <LinearLayout
            android:id="@+id/mid_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            ...>
            <TableLayout
                android:id="@+id/tiles_tables"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TableRow
                    android:id="@+id/row1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="3">
                    <ImageView
                        android:id="@+id/tile1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="3"
                        />
                    <ImageView
                        android:id="@+id/tile2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="3"
                        />

这里我得到了主要功能:

    ImageView[] tiles = new ImageView[9];
    for (int i = 0; i <= 8; i++) 
    {
    tiles[i].setImageResource(getResources().getIdentifier("tile_image1","drawable", getPackageName()));
    // this is just to get one image for all imageViews
    // here stars the problem
    tiles[i].getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
    tiles[i].getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
    }
ImageView[]tiles=新的ImageView[9];

对于(int i=0;i您可以将列表与
recyclerView
一起使用,并将
grid adapter
3列一起设置。
对于特定的
项目
,您需要使用
约束布局
作为
父项
,然后在
chlid
中为
图像
应用比率
1:1

<android.support.constraint.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="wrap_content">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintDimensionRatio="1:1"
    tools:src="@drawable/ic_chat_bubble_black_24dp"
    />

</android.support.constraint.ConstraintLayout>

是否希望在同一行中显示3幅高度和宽度相同的图像(不是静态的,而是根据分辨率)?嗨,thx。每行3张图片。9 x 9块。同样的宽度和高度。分辨率很好。我的意思是有不同的屏幕。所以它们必须拉伸。我不知道为什么它们之间有白色边框…所以问题是xml?你说?我需要9 x 9块。它们也应该拉伸不同的手机屏幕。实际上问题是你和我们之间的方式ed,您需要将recyclerview与GridLayoutManager一起使用,xml文件中也有一些修复,例如您必须使用ContrainLayout作为父级,然后您的图像将位于该布局中。之后,在设计选项卡中(一个是文本选项卡,另一个是studio中的设计选项卡)选择imagview,然后选择右侧,您获得了约束,然后您需要单击该约束打印的左上角,以便在特定行中为其提供相同大小的图像。简而言之,您可以得到您的解决方案上述解决方案适用于所有屏幕分辨率
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
  {
    View view = LayoutInflater
        .from(parent.getContext())
        .inflate(R.layout.item_list, null);

   int height = parent.getMeasuredHeight() / 4;
   int width = parent.getMeasuredWidth();

  view.setLayoutParams(new RecyclerView.LayoutParams(width, height));

 return new ViewHolder(view);
}