Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用水平线性布局时,Android元素被截断_Android_Android Layout_Android Linearlayout_Elements - Fatal编程技术网

使用水平线性布局时,Android元素被截断

使用水平线性布局时,Android元素被截断,android,android-layout,android-linearlayout,elements,Android,Android Layout,Android Linearlayout,Elements,我正在使用LinearLayout显示一些图像按钮。基本上我打算做的是给每个ImageButton一个特定的固定大小,然后根据屏幕大小,ImageButton的数量应该显示在一行中。理想情况下,在手机上,一行只有两个图像按钮 但是,当使用LinearLayout时,发生的情况是,不是将无法在一行上放置的额外图像按钮移动到下一行,而是将图像按钮切碎 代码如下: <LinearLayout android:orientation="vertical" android:layou

我正在使用LinearLayout显示一些图像按钮。基本上我打算做的是给每个ImageButton一个特定的固定大小,然后根据屏幕大小,ImageButton的数量应该显示在一行中。理想情况下,在手机上,一行只有两个图像按钮

但是,当使用LinearLayout时,发生的情况是,不是将无法在一行上放置的额外图像按钮移动到下一行,而是将图像按钮切碎

代码如下:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="What are you interested in?"
        android:gravity="center"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:gravity="center"
        android:padding="5px"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_margin="10dp"
            android:background="@drawable/education"
            android:id="@+id/imageButton" />

        <ImageButton
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_margin="10dp"
            android:background="@drawable/icon"
            android:id="@+id/imageButton2" />

        <ImageButton
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_margin="10dp"
            android:background="@drawable/icon"
            android:id="@+id/imageButton3" />

        <ImageButton
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_margin="10dp"
            android:background="@drawable/icon"
            android:id="@+id/imageButton4" />

    </LinearLayout>

</LinearLayout>



复制此代码并尝试

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:text="What are you interested in?" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:padding="5px"
        android:weightSum="4" >

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/icon" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/icon" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/icon" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/icon" />
    </LinearLayout>
</LinearLayout>

LinearLayout不会将内容从一行包装到另一行。我不认为SDK提供了一个可以做到这一点的工具,尽管我确信有一些第三方工具

你有几个选择

  • 只需在每行中放置较少的线(每行需要单独的线性布局)

  • 在运行时生成行,而不是在XML中静态声明行。这有点棘手,但您可以在代码中创建每个按钮,然后决定它是否适合当前行。如果没有,则创建新的LinearLayout并将其添加到总体布局中

  • 您可以创建自己的布局。这并不像看上去那么难。这项工作主要通过两种方法进行:onMeasure()和onLayout()

  • 顺便说一句,我认为您可能会遇到一些附带问题,因为您正在合并RelativeLayout(例如android:layout_alignParentTop=“true”)中与LinearLayout无关的标记。此外,对于LinearLayout,请确保指定方向

    下面是一个代码的略图(我省略了params,但这将为您提供一个可遵循的模型):

    LinearLayout outerLayout=(LinearLayout)findViewById(R.id.outer\u布局);
    LinearLayout rowLayout=null;
    int rowWidth=0;
    int screenWidth=获取屏幕宽度;
    对于(int i=0;i屏幕宽度)
    rowLayout=null;
    if(rowLayout==null)
    {
    rowLayout=新的LinearLayout();
    outerLayout.add(行布局);
    }
    rowLayout.add(b);
    rowWidth+=b.getIntrinsicWidth();
    }
    
    在实际布局之前,很难得到物体的尺寸。换句话说,您不能创建一个按钮然后使用getWidth()。它将返回0。getIntrinsicWidth()我相信可以,但不包括您可能需要的边距或其他调整。所以你必须手工添加这些

    或者,可以将元素渲染为位图,然后获取其尺寸。在您的情况下,第一种方法可能更简单、更充分


    为了方便起见,我省略了一些参数。代码太多,无法编写:)

    为每个ImageViewTested使用权重,但它会通过某种方式将所有四个ImageButton压缩在同一行上,看起来不太好看。非常感谢你的努力和帮助。@Anand Savjani…你还有什么建议吗?降低图像高度。我很想看看选项2是如何实现的,现在还不确定。你能详细说明一下吗?选项1是我最后的选择,虽然我想避免,因为即使是像平板电脑这样的大屏幕,它也只会显示2个按钮,我添加了一些几乎真实的代码。希望它能给你一个想法。到目前为止运气不好,在遇到一些问题之前还没有尝试过动态添加组件。。很快将共享代码@佩里哈特曼
    LinearLayout outerLayout = (LinearLayout)findViewById (R.id.outer_layout);
    LinearLayout rowLayout = null;
    int rowWidth = 0;
    int screenWidth = get screen width;
    for (int i = 0;  i < buttonCount;  i++)
    {
      ImageButton b = new ImageButton();
      b.setText();  b.... // other attributes;
    
      // add to current row
      if (rowLayout != null)
        if (b.getIntrinsicWidth() + rowWidth > screenWidth)
          rowLayout = null;
      if (rowLayout == null)
      {
        rowLayout = new LinearLayout();
        outerLayout.add (rowLayout);
      }
      rowLayout.add (b);
      rowWidth += b.getIntrinsicWidth();
    }