【发布时间】: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>
【问题讨论】:
-
发布一些图片的链接。我们会将图片放回此处。
-
如果您使用的是一堆按钮,那么您应该动态添加它。或者你可以使用 Gridview。
-
您还可以在 ScrollView 中包含您的 RelativeLayout。
-
使用gridview或tablelayout
标签: android xml android-layout layout android-relativelayout