2017-01-09 66 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> 

算了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" />