使用Visual Studio 2017在Xamarin上格式化布局

时间:2017-12-07 12:08:18

标签: android xamarin xamarin.android android-linearlayout

我正在尝试在我的移动应用上创建一个页面,其中显示了学生住宿列表。它应该显示地址,然后在下面有一个按钮链接到显示更多信息的页面。 我尝试过使用LinearLayout和RelativeLayout(见下文) Linear

Relative

我用来创建它的代码是

                    LinearLayout newLayout = new LinearLayout(Application.Context);
                TextView displayAddress = new TextView(Application.Context);
                displayAddress.Text = "Address: " + accomodation.address1 + ", " + accomodation.address2 + ", " +
                    accomodation.city + ", " + accomodation.postcode;
                newLayout.AddView(displayAddress);
                Button moreInformation = new Button(Application.Context);
                moreInformation.Text = "More Info";
                moreInformation.SetY(displayAddress.GetY() + 100);
                newLayout.AddView(moreInformation);
                mainLayout.AddView(newLayout);

有没有什么方法可以格式化任一布局,以便按钮位于文本下方?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以创建.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">
  <TextView
      android:id="@+id/textView_displayAddress"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Address:  accomodation.address1  accomodation.address2 accomodation.city  accomodation.postcode" />
  <Button
      android:id="@+id/moreInformation"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="moreInfo" />
</LinearLayout>

Effect