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

如何在Android布局中包装图像、按钮等内容?,android,wpf,android-layout,Android,Wpf,Android Layout,我想把按钮列表放在一个布局中,我想让它们自动流动。看看这张图片,这是如何在WPF中包装其内容的。有没有一种方法可以在Android中实现这一点 (来源:)你在安卓系统中使用了一种线性布局,它允许孩子们按照垂直或水平顺序进行布局。您将按钮放在线性布局中 您应该在android布局上完成此操作。链接中的绝对布局已取消种族划分 我对wrap panel的了解完全基于您所附的链接,因为我没有使用WPF。 这是我所理解的。 Android没有一个换行面板布局,当空间不足时,子视图会切换到下一行。你的选择

我想把按钮列表放在一个布局中,我想让它们自动流动。看看这张图片,这是如何在WPF中包装其内容的。有没有一种方法可以在Android中实现这一点



(来源:)

你在安卓系统中使用了一种线性布局,它允许孩子们按照垂直或水平顺序进行布局。您将按钮放在线性布局中

您应该在android布局上完成此操作。链接中的绝对布局已取消种族划分

我对wrap panel的了解完全基于您所附的链接,因为我没有使用WPF。 这是我所理解的。 Android没有一个换行面板布局,当空间不足时,子视图会切换到下一行。你的选择是

  • 使用ScrollView在视图大于屏幕时滚动

  • 对线性布局中的子视图使用布局权重属性。这将使视图成为
    调整自身大小以更改大小

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


前两个选项并不是您想要的。它们是android提供的替代方法。但如果你想要精确的东西,你需要自己制作。

这看起来像一个线性布局:

<?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

我想您需要使用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>


希望这有帮助!;)

-1线性布局不会包装内容。它只会把它们放在一起。如果空间不足,它不会做任何事情。谢谢你更新你的图片。上面的代码将创建您首先说的内容。但是,在移动设备上,您没有桌面上常见的调整窗口大小的情况。因此,几乎不需要流体布局。更好的用户体验是使用线性布局,其中所有按钮都具有同等的突出性。我只是更新了图像,而不是我的问题。问题和以前一样。-1线性布局不会包装内容。它只会把它们放在一起。如果没有足够的空间,它什么也做不了。首先,你要更新你的答案,给所有没有给出确切答案的人投负票。如果您想让布局在比屏幕大的情况下仍然可见,可以将linearlayout放在滚动视图中。android布局有一些限制,在wpf中所做的一切可能都不可能。我强烈建议您首先测试所有布局,并了解它们是如何协同工作的。blessenm是对的。您必须阅读Android通用布局对象()。。并了解他们是如何合作的:)我已经有了,但我做不到,这就是我在这里发布的原因。Android中的LinearLayout相当于Windows XAML中的StackPanel。根据下面@Master of None的回答,RelativePanel将取代LinearLayout,RelativePanel的性能无疑要比其他布局快得多,但仍然可以在运行时将数据放在代码中,并且数据几乎超过100项(PIC),因此,这可能很难处理。不,你正在破解它。我想让它自动流动,我们也可以使用LinearLayout来实现。RelativePanel的性能无疑比其他布局快得多,但如何在运行时将数据放在代码中,并且数据几乎超过100个项目(图片),因此可能很难处理。WrapePanel在Windows XAML中可用,因此我需要这样的功能。