私は画面の幅の25%になりたいと思うこのボタンを持っています。私はグリッドを作るいくつかの方法を見てきましたが、うまくいきませんでした。誰でも問題についての意見はありますか? これ以上コンテキストを記述する必要はありませんか?Xamarin C#画面の幅を%表示に設定します。Android
2
A
答えて
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
を追加することができます。
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
に設定しました。
関連する問題
- 1. Android用Xamarinフォーム全画面表示
- 2. android設定画面でダイアログを表示したい
- 3. Androidで希望の設定画面を表示する
- 4. 設定画面の概要を表示Android
- 5. Android画面の幅
- 6. Androidの設定方法オプションの選択と画面の表示
- 7. Androidは全画面表示のステータスバーを表示します
- 8. Xamarin Androidの全画面アクティビティでレイアウトが表示されない
- 9. Xamarinアプリはios11で黒い画面を表示します
- 10. XamarinのAndroidプロジェクトでImageViewの幅を動的に設定する
- 11. Androidアプリケーションの画面にログメッセージを表示
- 12. Android webviewの設定幅が画面幅よりも大きくならない
- 13. 設定画面にテキストブロックが表示されます
- 14. Android、レイアウト画面の幅
- 15. Android Nougat Webview画面の幅
- 16. Xamarin Android-Video playerブラック画面
- 17. オブジェクトの属性を画面の長さと幅に設定するC#
- 18. C#WinFormを別の画面に表示
- 19. WPFは、画面または親の幅の30%にデータグリッドを設定します
- 20. PCにAndroid画面を表示
- 21. Androidが画面に表示されている画面に項目を表示しています
- 22. CSS:画面の端に一致する幅のdivを設定します。
- 23. 角度フレックスレイアウトの固定画面幅を設定する
- 24. 画面にドロップダウンが表示されたときにオーバーレイを設定します。
- 25. ブルーインプの設定?全画面表示しない
- 26. setGesture()は、画面外のジェスチャーを表示します。Android
- 27. プログラムによる表示幅の設定
- 28. Android Widget:ウィジェットが画面に追加される前の設定アクティビティを表示します。
- 29. Androidの設定画面、全画面のみ?
- 30. アンドロイド:画面サイズに設定幅とビットマップベースの高さをプログラム
nice。 D –
それはあなたがすべての相対的なものを行い、あなたのレイアウトを平坦化し、幅とマージンのパーセンテージを行うことができるように、これはかなりきちんとしています。 – Cheesebaron
ワウのアスペクト比も同様です! –