【问题标题】:Xamarin android grid not scaling the cells proprely when an background image is added添加背景图像时,Xamarin android网格无法正确缩放单元格
【发布时间】:2017-07-28 03:20:38
【问题描述】:

我正在尝试使用 8 个不同的框(2 列 * 4 行)制作网格布局。不幸的是,每当我尝试加载图像时,它都会出错并加载我图像的升级版本。这是我的代码。必须注意,当我只是加载一些文字时,它工作得很好。

首先它看起来像这样: Without background image

使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:background="@drawable/garage">
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/background_dark" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/background_light" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/holo_green_dark" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/holo_orange_dark" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/holo_purple" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/holo_orange_light" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/holo_red_light" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@android:color/darker_gray" />
</GridLayout>

在我尝试在其中一个单元格中添加背景图像后,我得到的是:Only one cell that takes up more space than the whole screen as it looks

有以下代码:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:background="@drawable/garage">
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    android:background="@drawable/spaceshuttle1" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1" />
<TextView
    android:layout_columnWeight="1"
    android:layout_rowWeight="1" />
</GridLayout>

我该怎么做才能让图像留在它的单元格中?

【问题讨论】:

    标签: c# android xml xamarin


    【解决方案1】:

    如果参考TextView.onMeasure:

    measure 的基类实现默认为背景大小,除非 MeasureSpec 允许更大的大小。子类应覆盖 onMeasure(int, int) 以更好地测量其内容。

    所以如果你的背景图片很大,TextView 将被扩展为背景的大小。

    要解决这个问题,你可以创建自己的TextView,并覆盖onMeasure方法:

    public class MyTextView:TextView
    {
        public MyTextView(Context c) : base(c)
        {
        }
    
        public MyTextView(Context c, IAttributeSet attr) : base(c, attr)
        {
    
        }
    
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
    
            int parentWidth=MeasureSpec.GetSize(widthMeasureSpec);
            int parentHeight = MeasureSpec.GetSize(heightMeasureSpec);
            //set the size to the parent layout's cell's size.
            this.SetMeasuredDimension(parentWidth / 2, parentHeight / 4);
        }
    }
    

    并在您的布局中使用MyTextView

    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:rowCount="4">
      <YourNameSpace.MyTextView
          android:layout_column="1"
          android:layout_row="1"
          android:background="@drawable/beautifulchristmas"
       />
      <TextView
        android:layout_column="1"
        android:layout_row="2"
        android:background="@drawable/beautifulchristmas"/>
      ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多