2017-01-09 8 views
0

Как разделить экран на пять равных кругов. Я использую файл XML в качестве backgorung для круговРазделить экран на пять равных кругов

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
<shape android:shape="oval"> 
<solid android:color="#59d5fe"/> 
</shape> 
</item> 
</selector> 

и это код

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/img6" 
    android:weightSum="5" 
    android:orientation="horizontal" > 

    <ImageView 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:background="@drawable/vectorshape" 
    android:scaleType="fitXY" 
    android:id="@+id/img1" 
    android:layout_height="wrap_content"/> 
    <ImageView 
    android:layout_width="0dp" 
    android:background="@drawable/vectorshape" 
     android:scaleType="fitCenter" 
     android:layout_weight="1" 
     android:id="@+id/img2" 
    android:layout_height="wrap_content"/> 
    <ImageView 
    android:layout_width="0dp" 
    android:background="@drawable/vectorshape" 
    android:scaleType="fitCenter" 
    android:layout_weight="1" 
    android:id="@+id/img3" 
    android:layout_height="wrap_content"/> 
    <ImageView 
    android:layout_width="0dp" 
    android:background="@drawable/vectorshape" 
    android:scaleType="fitCenter" 
    android:id="@+id/img4" 
    android:layout_weight="1" 
    android:layout_height="wrap_content"/> 
    <ImageView 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:scaleType="fitCenter" 
     android:id="@+id/img5" 
    android:background="@drawable/vectorshape" 
    android:layout_height="wrap_content"/> 

</LinearLayout> 

</LinearLayout> 

Особенно я хочу размер высоты, чтобы быть таким же, как размер ширины, но давая пустой экран до тех пор, пока размер не будет определен в dp.

ответ

1

Поскольку ваша ImageView ширина зависит от ширины экрана. И вы не задали размер резкости.

Я думаю, что вам нужен квадрат ImageView, который должен быть определен самостоятельно с помощью следующего кода:

namespace SharpText_Demo 
{ 
    [Register("sharpText_Demo.SquareImageView")] 
    public class SquareImageView : ImageView 
    { 
     public SquareImageView(Context context):base(context) 
     { 

     } 

     public SquareImageView(Context context, IAttributeSet attrs):base(context,attrs,0) 
     { 

     } 

     public SquareImageView(Context context, IAttributeSet attrs, int defStyle):base(context,attrs,defStyle) 
     { 

     } 

     protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) 
     { 
      SetMeasuredDimension(GetDefaultSize(0, widthMeasureSpec), GetDefaultSize(0, heightMeasureSpec)); 
      int childWidthSize = MeasuredWidth; 
      //SET height = width 
      heightMeasureSpec = widthMeasureSpec = MeasureSpec.MakeMeasureSpec(childWidthSize, MeasureSpecMode.Exactly); 
      base.OnMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 
} 

Установите ImageView высота всегда равна его ширине.

Затем измените файл axml:

<?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"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:id="@+id/img6" 
     android:weightSum="5" 
     android:orientation="horizontal"> 
     <SharpText_Demo.SquareImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:background="@drawable/vectorshape" 
      android:id="@+id/img1" /> 
     <SharpText_Demo.SquareImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:scaleType="centerInside" 
      android:layout_weight="1" 
      android:background="@drawable/vectorshape" 
      android:id="@+id/img2" /> 
     <SharpText_Demo.SquareImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:scaleType="fitXY" 
      android:layout_weight="1" 
      android:background="@drawable/vectorshape" 
      android:id="@+id/img3" /> 
     <SharpText_Demo.SquareImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:scaleType="centerCrop" 
      android:id="@+id/img4" 
      android:background="@drawable/vectorshape" 
      android:layout_weight="1" /> 
     <SharpText_Demo.SquareImageView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:id="@+id/img5" 
      android:background="@drawable/vectorshape" /> 
    </LinearLayout> 
</LinearLayout> 

Forget scaleType используется для android:src = ....

вы получите снимок экрана:

enter image description here

+0

И как соединить его с деятельностью потому что я получил эту ошибку. Android.Views.InflateException: двоичная строка XML-файла # 1: двоичная строка XML-файла # 1: ошибка раздувания класса SharpText_Demo.Sq uareImageView на SetContentView – Merian

+0

Красивое объяснение, вероятно, было связано с тем, что в регистре SharpText_Demo.SquareImageView был с более низким s. Благодарю. – Merian

0

Вы можете сделать это, сделав свой vectorshape как изображение, например circle.png (width = height). И используя атрибут ImageViewandroid:adjustViewBounds="true" до wrap_content Ваш ImageView. Ваш ImageView так:

<ImageView 
      style="@style/imv_circle_group" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:adjustViewBounds="true" 
      android:src="@drawable/circle" /> 

 Смежные вопросы

  • Нет связанных вопросов^_^