【问题标题】:How to put image programmatically to match all layout?如何以编程方式放置图像以匹配所有布局?
【发布时间】:2020-01-14 09:00:18
【问题描述】:

我想匹配所有布局上的图像。

我在这里得到了一张图片:

我什么都试过了,这里是代码

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

我希望图像匹配所有平铺布局。

任何帮助将不胜感激。

【问题讨论】:

  • 您想在单行中以相同的高度和宽度(不是静态的,而是根据分辨率)的 3 张图像吗?
  • 嗨,谢谢。每行 3 张图像。 9 x 9 瓷砖。相同的宽度和高度。好分辨率..我的意思是有不同的屏幕..所以他们必须伸展。我不知道为什么我得到了它们之间的白色边框......

标签: android layout imageview


【解决方案1】:

您可以将列表与recyclerView 结合使用,并将grid adapter3 colums 结合使用。 对于特定item 的平方,您需要使用ContraintLayout 作为parent 然后在chlid 应用比率1:1image

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

或者你也可以参考这个

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

【讨论】:

  • 所以问题出在 xml 中?你说?
  • 我需要 9 x 9 的瓷砖。他们还应该针对不同的手机屏幕进行拉伸
  • 实际上问题出在您使用的方式上,您需要将 recyclerview 与 GridLayoutManager 一起使用,并且在 xml 文件中也有一些修复,例如您必须使用 contraintlayout 作为父级,然后您的图像将在其中布局。之后在“设计”选项卡(一个是文本选项卡,另一个是工作室中的设计选项卡)中选择 imagview,然后在右侧得到约束,然后您需要单击该约束打印的左上角,以便在特定行。简而言之,您可以获得您的解决方案
  • 上述解决方案适用于所有屏幕分辨率
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2022-10-19
相关资源
最近更新 更多