如何在Android布局中包装图像,按钮等内容?

时间:2011-10-12 16:41:25

标签: android wpf android-layout

我想在布局中放置按钮列表,我希望它们自动流动。看看这张图片,这是WrapPanel在WPF中包含其内容的方式。我有可能在Android中执行此操作吗?

This is what WrapPanel does in WPF http://i.msdn.microsoft.com/dynimg/IC163252.png

3 个答案:

答案 0 :(得分:2)

你在android中使用linearlayout,允许以垂直或水平顺序布局子项。您将按钮放在线性布局中。

你应该在android布局上查看这个tuorial。链接中的AbsoluteLayout被删除。

我对包装面板的了解完全基于您附加的链接,因为我没有使用WPF。 这是我所理解的。 Android没有包装面板布局,当空间较小时,子视图会突破到下一行。你的选择是

  • 如果视图大于屏幕,则使用滚动视图滚动。

  • 对linearlayout中的子视图使用layout_weight属性。这将使观点成为 根据不断变化的尺寸调整自己的大小。

  • 通过扩展ViewGroup类来创建自定义布局,您可以在其中测量子视图并根据可用的屏幕宽度确定其位置。

前两个选项并不完全符合您的要求。它们是android提供的替代方法。但如果你想要确切的东西,你需要自己做。

答案 1 :(得分:0)

这看起来像一个LinearLayout:

<?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">
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

有关在Android中使用XML布局的更多信息,请参阅android开发者网站上的这篇优秀介绍:http://developer.android.com/resources/tutorials/views/index.html

答案 2 :(得分:0)

我猜你需要使用RelativeLayout才能轻松实现。

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#c0c0c0"
    >
<Button 
    android:text="Button" 
    android:id="@+id/button3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    </Button>

<LinearLayout 
    android:id="@+id/linearlayout01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button3" 
    android:layout_alignParentLeft="true"
    android:orientation="horizontal"
    android:layout_marginRight="21dp"
    >
   <Button 
        android:text="Button" 
        android:layout_width="wrap_content" 
        android:layout_weight="2"
        android:id="@+id/button1" 
        android:layout_height="wrap_content"
        android:layout_gravity="left">
    </Button>
   <Button 
       android:text="Button" 
       android:layout_width="wrap_content" 
       android:id="@+id/button1" 
       android:layout_height="wrap_content" 
       android:layout_weight="2">
   </Button>
</LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/linearlayout01" 
    android:layout_alignParentLeft="true"
    android:weightSum="3"
    >
    <Button 
        android:text="Button" 
        android:layout_width="wrap_content" 
        android:id="@+id/button1" 
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </Button>
</LinearLayout>

希望这有帮助! ;)