首页 > 解决方案 > 如何在 Xamarin.Android 中创建视图

问题描述

在此处输入图像描述

如何在 Xamarin.Android 中制作这样的视图?

标签: xamarin.android

解决方案


您可以使用LinearLayoutGridLayout实现它。使用二LinearLayout,设置android:layout_weight=1两者,可以平分整个屏幕,然后使用GridLayout,添加所有奖项,用于Textview显示奖项名称,用于ImageView显示奖项图像。您可以android:background="@drawable/back"在外面设置为背景(奖架图像)LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent" 
    android:orientation="vertical"
     android:background="@drawable/back"
   >

    <GridLayout
       
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:rowCount="2"
        android:columnCount="6"
        android:orientation="vertical"
        >
    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="0">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

     
      

    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="1">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

    
    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="2">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

    
         <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="3">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

     
    
         <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="4">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

     
     <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="5">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

       
    
    
    </GridLayout>

     <GridLayout
        
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:rowCount="4"
        android:columnCount="6"
        android:orientation="vertical"
        >
    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="0">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

     
      

    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="1">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

    
    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="2">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

    
         <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="3">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

     
    
         <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="4">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

     
     <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_row="0"
        android:layout_column="5">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hamberguer1"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/myicon"/>
     </LinearLayout>

       
    
    
    </GridLayout>
</LinearLayout>

这是运行的gif。

在此处输入图像描述

您需要为shelves. 这是代码。

    public class BookGridView: Android.Widget.GridView
    {
        Context context;
        private Bitmap background;
        public BookGridView(Context context, Android.Util.IAttributeSet attributeSet):base(context, attributeSet)
        {
            
            this.context = context;
            background = null;
        }


        protected override void DispatchDraw(Canvas canvas)
        {
           
            int count = ChildCount;
            int top = count > 0 ? GetChildAt(0).Top : 0;

            int width = Width;
            int height = Height;

            int totalCount = this.Count;

            //If the total is 0, there is no need to calculate the height and draw the picture
            if (totalCount > 0)
            {

                //get Columns
                int numColumns = 3;
          // Calculate the row height by calculating the number of rows and height
                int rowCount = totalCount / numColumns;
                if (totalCount % numColumns != 0)
                {
                    rowCount++;
                }
                int rowHeight = height / rowCount;


                if (background == null)
                {
                    background = BitmapFactory.DecodeResource(Resources,
                            Resource.Drawable.bookshelf_layer_center);
                    background = Bitmap.CreateScaledBitmap(background, width, rowHeight, true);
                }

                int backgroundWidth = background.Width;
                int backgroundHeight = background.Height;

                for (int y = top; y < height; y += backgroundHeight)
                {
                    for (int x = 0; x < width; x += backgroundWidth)
                    {
                        canvas.DrawBitmap(background, x, y, null);
                    }
                }
            }
            base.DispatchDraw(canvas);
        }



    }
}

这是运行截图。上架奖励的数量会增加。

在此处输入图像描述

我上传了我的demo,你可以自己设置货架的列。

https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/App17.zip


推荐阅读