簡體   English   中英

如何在LinearLayout中將所有項目保持相同的大小,同時防止它們超出Xamarin.Android中的布局范圍?

[英]How do you keep all of the items in a LinearLayout the same size while preventing them from going out of the bounds of the layout in Xamarin.Android?

目標 :我試圖以編程方式將50到75個RelativeLayouts添加到垂直的LinearLayout中。 每個RelativeLayout內部都有另一個LinearLayout,其中包含2個ImageViews。 我希望所有圖像都可見且大小相同。

問題 :當我嘗試添加它們時,僅可見40個ImageView,其余的未顯示。

  • 每個RelativeLayout包含2個ImageViews,其中包含一個寬圖像。
  • 所有ImageView上的所有圖像都具有相同的高度和寬度。
  • 只要顯示所有圖像,寬高比是否丟失都沒有關系。
  • 只有頂部的LinearLayout具有邊距。 里面的所有視圖都沒有空白。

圖:

我的適配器 (不擴展任何本機適配器)

        private void LoadViews()
        {
            int amountOfRowsPerYear = 52 / mmCalc.amountOfWeeksInARow;

            for (int i = 0; i <= artifact.RowMetaList.Count; i += amountOfRowsPerYear)
            {
                cvWrapper = GetView(i, amountOfRowsPerYear);
                parent.AddView(cvWrapper);
            }

        }

        private RelativeLayout GetView(int position, int amountOfRowsPerYear)
        {
            LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
            RelativeLayout convertView = (RelativeLayout)inflater.Inflate(Resource.Layout.ArtifactRowLayout, null);

            if (amountOfRowsPerYear != 2)
                throw new NotImplementedException("this mehtod can only accept 2 rows per year for now");
            else if (amountOfRowsPerYear == 2)
            {
                int first = position;
                int second = position + 1;
                Artifact.RowMetaData row = artifact.RowMetaList[first];
                Artifact.RowMetaData row2 = null;
                try {row2 = artifact.RowMetaList[second];}catch (Exception ex){}
                ImageView RowImageView = convertView.FindViewById<ImageView>(Resource.Id.RowImageView);
                ImageView RowImageView2 = convertView.FindViewById<ImageView>(Resource.Id.RowImageView2);

                RowImageView.SetImageBitmap(GetRowBitmap(row));
                try { RowImageView2.SetImageBitmap(GetRowBitmap(row2)); } catch (Exception ex)
                { RowImageView2.Visibility = ViewStates.Gone; }
            }
            return convertView;
        }

ArtifactRowLayout.axml (在上面的GetView()方法中膨脹的布局)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RowMainRL"
android:minWidth="25px"
android:minHeight="25px"
android:layout_weight="1.0">
<LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout1">
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/RowImageView"
        android:adjustViewBounds="false" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/RowImageView2"
        android:adjustViewBounds="false"
        android:src="@android:drawable/ic_menu_gallery" />
</LinearLayout>

ArtifactDisplayer.axml (在id / ArtifactLinearLayout中是視圖的放大位置)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:animateLayoutChanges="true"
    android:theme="@style/MyTheme">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ArtifactLinearLayout"
        android:layout_margin="20dp" />
</LinearLayout>

我已經在所有視圖上嘗試了許多不同的屬性,但是找不到解決方案。 超過一半的視圖最終超出范圍。

我還嘗試以編程方式設置每個視圖的確切高度和寬度,但是由於某些原因,這樣做后它們不可見。

您不會將它們全部顯示出來,除非它們太小了以至於它們無法全部顯示在屏幕上。

正確的答案是使用ListView或RecyclerView,以便您可以有效滾動並顯示所有內容。 一個糟糕但可行的解決方案是將所有內容都轉儲到ScrollView中,以便它可以滾動並顯示所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM