2016-04-05 32 views
2

私は画面の幅の25%になりたいと思うこのボタンを持っています。私はグリッドを作るいくつかの方法を見てきましたが、うまくいきませんでした。誰でも問題についての意見はありますか? これ以上コンテキストを記述する必要はありませんか?Xamarin C#画面の幅を%表示に設定します。Android

答えて

5

Android.Support.Designパッケージには、PercentRelativeLayoutという新しいレイアウトタイプが用意されています。

Xamarin.Android.Support.Percent NuGet packageをプロジェクトに追加すると表示されます。あなたはmore information about this layout in the Google API docsを見つけることができます

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
    <ImageView 
     app:layout_widthPercent="50%" 
     app:layout_heightPercent="50%" 
     app:layout_marginTopPercent="25%" 
     app:layout_marginLeftPercent="25%" /> 
    <!-- more views here --> 
</android.support.percent.PercentRelativeLayout> 

これは次のようにあなたのレイアウトにPercentRelativeLayoutを追加することができます。

+0

nice。 D –

+1

それはあなたがすべての相対的なものを行い、あなたのレイアウトを平坦化し、幅とマージンのパーセンテージを行うことができるように、これはかなりきちんとしています。 – Cheesebaron

+0

ワウのアスペクト比も同様です! –

2

水平のLinearLayoutを使用し、ボタンのlayout_weightを.25に設定し、重さが.75の空のビューを追加できます。注:

public class SquareLayout : LinearLayout 
{ 
    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     var width = MeasureSpec.GetSize(widthMeasureSpec); 
     var height = MeasureSpec.GetSize(heightMeasureSpec); 

     if (height == 0 && width == 0) 
     { 
      base.OnMeasure(widthMeasureSpec, heightMeasureSpec); 
      return; 
     } 

     if (height == 0) 
     { 
      height = width; 
     } 
     else if (width == 0) 
     { 
      width = height; 
     } 

     if (width > height) 
      width = height; 
     else 
      height = width; 

     base.OnMeasure(
      MeasureSpec.MakeMeasureSpec(width, MeasureSpecMode.Exactly), 
      MeasureSpec.MakeMeasureSpec(height, MeasureSpecMode.Exactly) 
     ); 
    } 

    protected SquareLayout(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) 
    { 
    } 

    public SquareLayout(Context context) : base(context) 
    { 
    } 

    public SquareLayout(Context context, IAttributeSet attrs) : base(context, attrs) 
    { 
    } 

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

そして、あなたのAXMLに

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <mynamespacelowercase.SquareLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25"> 
     <Button 
      android:text="MyButton" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    <mynamespacelowercase.SquareLayout> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".75" /> 

</LinearLayout> 
をあなたのレイアウトを使用する:あなたは何かが乗、あなたのようなカスタムレイアウトを作成する必要がある場合 layout_width

0に
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <Button 
     android:text="MyButton" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".75" /> 
</LinearLayout> 

を設定します。

また、カスタムレイアウトを変更して、パーセンテージに応じて幅を計算することもできます。次に、空のビューとレイアウトの重みは必要ありません。

0

ありがとうございます。

これが最も簡単だとわかりました。

var metric = Resources.DisplayMetrics; 
width = (int)(metric.WidthPixels * .25); 

次に、幅をLayoutParametersに設定しました。

関連する問題