Xamarin.Android布局占用所有可用space?
Xamarin.Android layout to occupy all the available space?
请帮我实现这个布局,我需要 3 个 ImageView 和一个 TextView 在同一行,最后两个对齐到右侧。我试过将它们放入 LinearLayout、GridLayout、TableLayout、RelativeLayout,但天哪,我无法弄清楚!
谢谢!
线性布局(比例):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_margin="5dp"
android:src="@drawable/ic_launcher_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<TextView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="50dp"
android:background="@color/colorAccent"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"/>
<ImageView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="@drawable/ic_launcher_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<ImageView
android:layout_margin="5dp"
android:src="@drawable/ic_launcher_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
</LinearLayout>
RelativeLayout(无比例):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_margin="5dp"
android:id="@+id/iv1"
android:src="@drawable/ic_launcher_background"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_toRightOf="@id/iv1"
android:id="@+id/tv"
android:text="TextView"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/colorAccent"/>
<ImageView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_toLeftOf="@id/iv3"
android:id="@+id/iv2"
android:src="@drawable/ic_launcher_background"
android:layout_width="50dp"
android:layout_height="50dp" />
<ImageView
android:layout_margin="5dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:id="@+id/iv3"
android:src="@drawable/ic_launcher_background"
android:layout_width="50dp"
android:layout_height="50dp" />
</RelativeLayout>
更新:
var relativeLayout = new Android.Widget.RelativeLayout(this)
{
LayoutParameters = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent),
LayoutDirection = LayoutDirection.Ltr
};
var imageView1 = new ImageView(this);
imageView1.SetImageResource(Resource.Drawable.icon);
var layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.SetMargins(5, 5, 5, 5);
imageView1.Id = 1;
imageView1.LayoutParameters = layoutParams;
relativeLayout.AddView(imageView1);
View childView1 = relativeLayout.GetChildAt(0);
var textView = new TextView(this)
{
Text = "TextView"
};
layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.AddRule(LayoutRules.RightOf, childView1.Id);
layoutParams.SetMargins(0, 5, 5, 5);
textView.Id = 2;
textView.LayoutParameters = layoutParams;
relativeLayout.AddView(textView);
var imageView3 = new ImageView(this);
imageView3.SetImageResource(Resource.Drawable.icon);
layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.AddRule(LayoutRules.AlignParentEnd);
layoutParams.AddRule(LayoutRules.AlignParentRight);
imageView3.Id = 3;
imageView3.LayoutParameters = layoutParams;
relativeLayout.AddView(imageView3);
View childView2 = relativeLayout.GetChildAt(2);
var imageView2 = new ImageView(this);
imageView2.SetImageResource(Resource.Drawable.icon);
layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.AddRule(LayoutRules.LeftOf, childView2.Id);
imageView2.Id = 4;
imageView2.LayoutParameters = layoutParams;
relativeLayout.AddView(imageView2);
请帮我实现这个布局,我需要 3 个 ImageView 和一个 TextView 在同一行,最后两个对齐到右侧。我试过将它们放入 LinearLayout、GridLayout、TableLayout、RelativeLayout,但天哪,我无法弄清楚!
谢谢!
线性布局(比例):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_margin="5dp"
android:src="@drawable/ic_launcher_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<TextView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="50dp"
android:background="@color/colorAccent"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"/>
<ImageView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="@drawable/ic_launcher_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
<ImageView
android:layout_margin="5dp"
android:src="@drawable/ic_launcher_background"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp" />
</LinearLayout>
RelativeLayout(无比例):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_margin="5dp"
android:id="@+id/iv1"
android:src="@drawable/ic_launcher_background"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_toRightOf="@id/iv1"
android:id="@+id/tv"
android:text="TextView"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/colorAccent"/>
<ImageView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_toLeftOf="@id/iv3"
android:id="@+id/iv2"
android:src="@drawable/ic_launcher_background"
android:layout_width="50dp"
android:layout_height="50dp" />
<ImageView
android:layout_margin="5dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:id="@+id/iv3"
android:src="@drawable/ic_launcher_background"
android:layout_width="50dp"
android:layout_height="50dp" />
</RelativeLayout>
更新:
var relativeLayout = new Android.Widget.RelativeLayout(this)
{
LayoutParameters = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent),
LayoutDirection = LayoutDirection.Ltr
};
var imageView1 = new ImageView(this);
imageView1.SetImageResource(Resource.Drawable.icon);
var layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.SetMargins(5, 5, 5, 5);
imageView1.Id = 1;
imageView1.LayoutParameters = layoutParams;
relativeLayout.AddView(imageView1);
View childView1 = relativeLayout.GetChildAt(0);
var textView = new TextView(this)
{
Text = "TextView"
};
layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.AddRule(LayoutRules.RightOf, childView1.Id);
layoutParams.SetMargins(0, 5, 5, 5);
textView.Id = 2;
textView.LayoutParameters = layoutParams;
relativeLayout.AddView(textView);
var imageView3 = new ImageView(this);
imageView3.SetImageResource(Resource.Drawable.icon);
layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.AddRule(LayoutRules.AlignParentEnd);
layoutParams.AddRule(LayoutRules.AlignParentRight);
imageView3.Id = 3;
imageView3.LayoutParameters = layoutParams;
relativeLayout.AddView(imageView3);
View childView2 = relativeLayout.GetChildAt(2);
var imageView2 = new ImageView(this);
imageView2.SetImageResource(Resource.Drawable.icon);
layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layoutParams.AddRule(LayoutRules.LeftOf, childView2.Id);
imageView2.Id = 4;
imageView2.LayoutParameters = layoutParams;
relativeLayout.AddView(imageView2);