【问题标题】:Linearlayout should not cut off线性布局不应该被切断
【发布时间】:2013-09-26 13:24:49
【问题描述】:

抱歉这个新手问题。我google了一段时间,找不到答案。

我连续有 10 个按钮。这在我的平板电脑上看起来不错。但是当我在手机上启动应用程序时,只显示前 5 个按钮,其余的都被切断了。

如果没有足够的空间,剩余的按钮应该显示在第二行。我该怎么做?

谢谢。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1"
/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="4"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="7"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="8"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="9"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10"
    />

【问题讨论】:

  • 我认为没有简单的方法可以做到这一点。
  • 一个网格视图,可能是,但你的按钮都在同一边。
  • 使用“ScrollView”并将您的 LinearLayout 放入 scrollView - example
  • 当我使用GridView时,我应该在GridView里面使用Button还是ImageView?

标签: android android-layout android-linearlayout android-button


【解决方案1】:

有趣!取scrollview父母LinearLayout@

<ScrollView...
<LinearLayout...//android:orientation="horizontal"
<Button... // your all buttons
</Button>  
</LinearLayout>
</ScrollView>

【讨论】:

    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        />
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        />
    </LinearLayout>
    </ScrollView>
    

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      你有几个选择:

      1. 将所有按钮放入GridView - 这适用于任何屏幕尺寸,并且相对简单明了。

      2. 将它们放入ScrollView。这很流行,但对用户来说有点烦人,因为它可能导致误点击,而且他们可能没有意识到您可以滚动按钮。

      3. 如果您只需要 1 或 2 行,具体取决于屏幕大小,请将每行按钮放入单独的布局文件中。然后创建 2 个包含 LinearLayout 的附加屏幕相关布局,其中包括两行,但为纵向版本提供垂直方向。因此,当加载该布局时,您会得到两行。

      4. 确定您是否可以以不同的方式呈现按钮 - 可能只使用图标,或者将它们组合成下拉列表。

      【讨论】:

      • 你好,我在使用GridView的时候,应该在GridView里面使用Button还是ImageView?
      • 如果它们是文本按钮,则使用Button。如果它们是由一个图像组成的按钮,请使用ImageButton。如果它们只是图像 - 不应该被触摸/点击 - 使用 ImageView
      【解决方案5】:
      // try this way
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    tools:context=".MainActivity">
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="1"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="2"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="3"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="4"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="5"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="6"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="7"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="8"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="9"/>
      
          <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1.2"
                  android:text="10"/>
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多