私は画面の幅の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. 設定画面にテキストブロックが表示されます
- 7. Androidアプリケーションの画面にログメッセージを表示
- 8. Android、レイアウト画面の幅
- 9. Androidが画面に表示されている画面に項目を表示しています
- 10. PCにAndroid画面を表示
- 11. プログラムによる表示幅の設定
- 12. Xamarin Android - テキストビューを非表示にする
- 13. setGesture()は、画面外のジェスチャーを表示します。Android
- 14. Android Widget:ウィジェットが画面に追加される前の設定アクティビティを表示します。
- 15. xamarin androidの画像をフルスクリーンで表示する方法は?
- 16. Androidの設定画面、全画面のみ?
- 17. 画面幅に応じてdivを中心に設定
- 18. Android(スプラッシュスクリーン)の黒い画面表示
- 19. Android webview div style = "height:auto"は黒い画面を表示します
- 20. Android - 画面サポートを無効にするx大画面を表示
- 21. XMLの別の設定画面内の別のAndroid設定画面を参照してください
- 22. Androidの設定リストの高さは画面の解像度に応じて表示されます
- 23. Swift 2を使用して、MMDrawerControllerの引き出し幅を画面の幅に設定する方法は?
- 24. Cocoaアプリで全画面表示に設定されていますか?
- 25. Pythonのtkinter画面の幅と高さの副表示
- 26. 画像の幅とdivの幅を画面のサイズに合わせて設定する
- 27. Android Gallery Viewは1つの画面に2つのビューを表示します。
- 28. MVC3剃刀ブラウザ画面の幅と高さの表示
- 29. Androidの同じ画面にビデオとボタンを表示する
- 30. すべてのホーム画面にAndroid Appウィジェットを表示
nice。 D –
それはあなたがすべての相対的なものを行い、あなたのレイアウトを平坦化し、幅とマージンのパーセンテージを行うことができるように、これはかなりきちんとしています。 – Cheesebaron
ワウのアスペクト比も同様です! –