【问题标题】:RelativeLayout doesn't show all buttonsRelativeLayout 不显示所有按钮
【发布时间】:2014-04-18 01:41:02
【问题描述】:

我无法将所有按钮都放在屏幕上?最后一行半的按钮不在屏幕上?最终布局看起来有点不同我已经制作了自定义按钮,并且大多数按钮都有辅助功能,但我已经简化了代码以消除所有可能与我的问题无关的错误。我希望按钮既能填充又能留在屏幕上(我会发布我的布局图片,但我的声誉太低了)

相对布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none" />

    <ImageView
        android:id="@+id/sample"
        android:layout_below="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/contentDescriptionImageView"
        android:scaleType="centerInside"
        android:src="@drawable/sample" />

    <Button
        android:id="@+id/b11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sample"
        android:layout_margin="0dp"
        android:text="@string/b11" />

    //...buttons

    <Button
        android:id="@+id/b95"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/b85"
        android:layout_margin="0dp"
        android:layout_toRightOf="@+id/b94"
        android:text="@string/b95" />

</RelativeLayout>

LinearLayout(LinearLayout+GridLayout)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayoutH1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="none" />

        <ImageView
            android:id="@+id/sample"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/contentDescriptionImageView"
            android:scaleType="centerInside"
            android:src="@drawable/sample" />
    </LinearLayout>

    <GridLayout
        android:id="@+id/GridLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="5"
        android:rowCount="9"
        android:useDefaultMargins="false" >

        <Button
            android:id="@+id/b11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:text="@string/b11" />

        //...buttons are the same, I just erased the following RelativeLayout attributes:
-android:layout_below
-android:layout_toRightOf
-android:layout_alignParentBottom

        <Button
            android:id="@+id/b95"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:text="@string/b95" />

    </GridLayout>

</LinearLayout>

http://postimg.org/image/wvcl7rfer/eeef02dd/

这是对我有用的解决方案(我没有尝试过 GridView 或 TableLayout) 这是BrOtherSOul的那个。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
    android:orientation="vertical" >

<EditText
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="none" />

<ImageView
    android:id="@+id/sample"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/contentDescriptionImageView"
    android:scaleType="centerInside"
    android:src="@drawable/sample" />

<LinearLayout
    android:id="@+id/linearLayoutH3"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b11" />

    <Button
        android:id="@+id/b12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b12" />

    <Button
        android:id="@+id/b13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b13" />

    <Button
        android:id="@+id/b14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b14" />

    <Button
        android:id="@+id/b15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b15" />


</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH5"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b21" />

    <Button
        android:id="@+id/b22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b22" />

    <Button
        android:id="@+id/b23"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b23" />

    <Button
        android:id="@+id/b24"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b24" />

    <Button
        android:id="@+id/b25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b25" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH7"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b31"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b31" />

    <Button
        android:id="@+id/b32"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b32" />

    <Button
        android:id="@+id/b33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b33" />

    <Button
        android:id="@+id/b34"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b34" />

    <Button
        android:id="@+id/b35"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b35" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH9"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b41"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b41" />

    <Button
        android:id="@+id/b42"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b42" />

    <Button
        android:id="@+id/b43"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b43" />

    <Button
        android:id="@+id/b44"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b44" />

    <Button
        android:id="@+id/b45"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b45" />


</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH11"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b51"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b51" />

    <Button
        android:id="@+id/b52"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b52" />

    <Button
        android:id="@+id/b53"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b53" />

    <Button
        android:id="@+id/b54"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b54" />

    <Button
        android:id="@+id/b55"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b55" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH13"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b61"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b61" />

    <Button
        android:id="@+id/b62"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b62" />

    <Button
        android:id="@+id/b63"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b63" />

    <Button
        android:id="@+id/b64"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b64" />

    <Button
        android:id="@+id/b65"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b65" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH15"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b71"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b71" />

    <Button
        android:id="@+id/b72"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b72" />

    <Button
        android:id="@+id/b73"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b73" />

    <Button
        android:id="@+id/b74"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b74" />

    <Button
        android:id="@+id/b75"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b75" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH17"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b81"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b81" />

    <Button
        android:id="@+id/b82"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b82" />

    <Button
        android:id="@+id/b83"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b83" />

    <Button
        android:id="@+id/b84"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b84" />

    <Button
        android:id="@+id/b85"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b85" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayoutH19"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <Button
        android:id="@+id/b91"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b91" />

    <Button
        android:id="@+id/b92"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b92" />

    <Button
        android:id="@+id/b93"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b93" />

    <Button
        android:id="@+id/b94"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b94" />

    <Button
        android:id="@+id/b95"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:text="@string/b95" />

</LinearLayout>

</LinearLayout>

http://postimg.org/image/ltromjjpp/5ad91d61/

【问题讨论】:

  • 发布一些图片的链接。我们会将图片放回此处。
  • 如果您使用的是一堆按钮,那么您应该动态添加它。或者你可以使用 Gridview。
  • 您还可以在 ScrollView 中包含您的 RelativeLayout。
  • 使用gridview或tablelayout

标签: android xml android-layout layout android-relativelayout


【解决方案1】:

使用滚动视图老兄

        <ScrollView
               xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
               android:layout_height="fill_parent">
            <RelativeLayout
             android:id="@+id/MainLayout"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >

        // here u can add your all button or edit text or whatever u want to add just add 

        </ RelativeLayout>
        </ScrollView>

【讨论】:

  • 你应该摆脱无用的外部RelativeLayout。
  • 是的,但这是针对 @Mathieu_Roth 的,即 Mathieu_Roth 可以准确识别出滚动视图的位置,当 Mathieu_Roth 应用此代码时,Mathieu_Roth 会自动意识到这一点:)
  • 它没用...并降低了布局的性能。只需将原始的 RelativeLayout 移动到 ScrollView 内(然后不需要 LinearLayout)。或者根据您的喜好删除外部的 RelativeLayout 并保留 LinearLayout。 ScrollView 必须是第一个容器
  • 将线性布局与 android:orientation="vertical" 一起使用,即所有布局视图都自动位于下方,并且不需要像 android:layout_below="" 这样的任何参考
  • 但是你放弃了android:layout_toRightOf="@+id/b11" 的使用(以及所有其他相对定位)。他正在制作按钮列。
【解决方案2】:

像这样更新你的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/MainLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/result"
            style="@style/ButtonTextWhite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="none" />

        <ImageView
            android:id="@+id/sample"
            style="@style/ButtonTextWhite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/result"
            android:contentDescription="@string/contentDescriptionImageView"
            android:scaleType="centerInside"
            android:src="@drawable/sample" />

        <Button
            android:id="@+id/b11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sample"
            android:layout_margin="0dp"
            android:text="@string/b11" />

        <Button
            android:id="@+id/b12"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sample"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b11"
            android:text="@string/b12" />

        <Button
            android:id="@+id/b13"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sample"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b12"
            android:text="@string/b13" />

        <Button
            android:id="@+id/b14"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sample"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b13"
            android:text="@string/b14" />

        <Button
            android:id="@+id/b15"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sample"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b14"
            android:text="@string/b15" />

        <Button
            android:id="@+id/b21"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b11"
            android:layout_margin="0dp"
            android:text="@string/b21" />

        <Button
            android:id="@+id/b22"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b12"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b21"
            android:text="@string/b22" />

        <Button
            android:id="@+id/b23"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b13"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b22"
            android:text="@string/b23" />

        <Button
            android:id="@+id/b24"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b14"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b23"
            android:text="@string/b24" />

        <Button
            android:id="@+id/b25"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b15"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b24"
            android:text="@string/b25" />

        <Button
            android:id="@+id/b31"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b21"
            android:layout_margin="0dp"
            android:text="@string/b31" />

        <Button
            android:id="@+id/b32"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b22"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b31"
            android:text="@string/b32" />

        <Button
            android:id="@+id/b33"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b23"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b32"
            android:text="@string/b33" />

        <Button
            android:id="@+id/b34"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b24"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b33"
            android:text="@string/b34" />

        <Button
            android:id="@+id/b35"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b25"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b34"
            android:text="@string/b35" />

        <Button
            android:id="@+id/b41"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b31"
            android:layout_margin="0dp"
            android:text="@string/b41" />

        <Button
            android:id="@+id/b42"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b32"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b41"
            android:text="@string/b42" />

        <Button
            android:id="@+id/b43"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b33"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b42"
            android:text="@string/b43" />

        <Button
            android:id="@+id/b44"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b34"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b43"
            android:text="@string/b44" />

        <Button
            android:id="@+id/b45"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b35"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b44"
            android:text="@string/b45" />

        <Button
            android:id="@+id/b51"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b41"
            android:layout_margin="0dp"
            android:text="@string/b51" />

        <Button
            android:id="@+id/b52"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b42"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b51"
            android:text="@string/b52" />

        <Button
            android:id="@+id/b53"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b43"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b52"
            android:text="@string/b53" />

        <Button
            android:id="@+id/b54"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b44"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b53"
            android:text="@string/b54" />

        <Button
            android:id="@+id/b55"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b45"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b54"
            android:text="@string/b55" />

        <Button
            android:id="@+id/b61"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b51"
            android:layout_margin="0dp"
            android:text="@string/b61" />

        <Button
            android:id="@+id/b62"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b52"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b61"
            android:text="@string/b62" />

        <Button
            android:id="@+id/b63"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b53"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b62"
            android:text="@string/b63" />

        <Button
            android:id="@+id/b64"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b54"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b63"
            android:text="@string/b64" />

        <Button
            android:id="@+id/b65"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b55"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b64"
            android:text="@string/b65" />

        <Button
            android:id="@+id/b71"
            style="@style/ButtonTextWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b61"
            android:layout_margin="0dp"
            android:text="@string/b71" />

        <Button
            android:id="@+id/b72"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b62"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b71"
            android:text="@string/b72" />

        <Button
            android:id="@+id/b73"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b63"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b72"
            android:text="@string/b73" />

        <Button
            android:id="@+id/b74"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b64"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b73"
            android:text="@string/b74" />

        <Button
            android:id="@+id/b75"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b65"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b74"
            android:text="@string/b75" />

        <Button
            android:id="@+id/b81"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b71"
            android:layout_margin="0dp"
            android:text="@string/b81" />

        <Button
            android:id="@+id/b82"
            style="@style/ButtonTextBlack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b72"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b81"
            android:text="@string/b82" />

        <Button
            android:id="@+id/b83"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b73"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b82"
            android:text="@string/b83" />

        <Button
            android:id="@+id/b84"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b74"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b83"
            android:text="@string/b84" />

        <Button
            android:id="@+id/b85"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/b75"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b84"
            android:text="@string/b85" />

        <Button
            android:id="@+id/b91"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/b81"
            android:layout_margin="0dp"
            android:text="@string/b91" />

        <Button
            android:id="@+id/b92"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/b82"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b91"
            android:text="@string/b92" />

        <Button
            android:id="@+id/b93"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/b83"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b92"
            android:text="@string/b93" />

        <Button
            android:id="@+id/b94"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/b84"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b93"
            android:text="@string/b94" />

        <Button
            android:id="@+id/b95"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/b85"
            android:layout_margin="0dp"
            android:layout_toRightOf="@+id/b94"
            android:text="@string/b95" />
    </RelativeLayout>

</ScrollView>

【讨论】:

  • 感谢您的快速解答。 Scrollview 工作正常,遗憾的是,考虑到我为这个项目提供的规格,滚动不是一个选项
【解决方案3】:

对每三个按钮使用线性布局,这样您就可以轻松实现您的要求。

<linearlayout

orientation=horizantal>
<button1>
<button2>
<button3>

</linearlayout>

这样使用...

【讨论】:

    【解决方案4】:

    只需在相对布局之前添加滚动视图

    【讨论】:

    • 感谢您的快速解答。 Scrollview 工作正常,遗憾的是,考虑到我为这个项目提供的规格,滚动不是一个选项
    【解决方案5】:

    只需将您的相对布局包装在 ScrollView 中即可。

    【讨论】:

    • 感谢您的快速解答。 Scrollview 工作正常,遗憾的是,考虑到我为这个项目提供的规格,滚动不是一个选项
    【解决方案6】:

    第一个:

    永远不要使用 XML 在布局中创建大量按钮。如果您想生成大按钮,请执行dynamically 或使用GridView

    第二个: 如果您在 Xml 中创建大量按钮,请不要忘记使用 ScrollView

     <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <RelativeLayout
            android:id="@+id/MainLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
                // Your buttons
    
        </RelativeLayout>
    
    </ScrollView>
    

    【讨论】:

      【解决方案7】:

      您要求您的GridLayout 将其内容包装在垂直侧。它确实做到了这一点,并且必须超越屏幕才能做到这一点。 GridLayout 旨在防止嵌套布局,但看起来您必须这样做。尝试放弃GridLayout 并为每一行按钮创建一个LinearLayout,所有这些都在LinearLayout 中。 我知道这很脏但是......(他们应该支持体重) 该解决方案适用于您将在其上运行的每个屏幕的垂直侧。你可以对宽度做同样的事情(我刚刚解决了垂直问题)

      像这样:

      <?xml version="1.0" encoding="utf-8"?>
      
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/MainLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
      
      <EditText
          android:id="@+id/result"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="none" />
      
      <ImageView
          android:id="@+id/sample"
          android:layout_below="@+id/result"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:contentDescription="@string/contentDescriptionImageView"
          android:scaleType="centerInside"
          android:src="@drawable/sample" />
      
      <LinearLayout
          android:id="@+id/1stRow"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1" >
      
       //.... Your buttons
      
      <LinearLayout
          android:id="@+id/2ndRow"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1" >
      
       //.... Buttons again
      
      <LinearLayout
          android:id="@+id/LastRow"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1" >
      
      </LinearLayout>
      

      真诚地, 你的救星:)

      【讨论】:

        猜你喜欢
        • 2018-08-13
        • 2021-05-28
        • 2012-05-15
        • 1970-01-01
        • 2017-01-18
        • 1970-01-01
        • 1970-01-01
        • 2021-10-15
        相关资源
        最近更新 更多