Android 按钮大小调整问题。安卓
在我的项目中,我有一个带有4个按钮的视图。我有一个相对的布局来正确放置它们。问题是,我希望按钮能够填充整个屏幕而不会消失,我不希望为它们设置固定的大小,以确保按钮能够适应任何屏幕大小。现在我唯一能看到的是一个大按钮,因为RelativeLayout没有android:layout\u weight。我怎样才能做到这一点?以下是我得到的:Android 按钮大小调整问题。安卓,android,Android,在我的项目中,我有一个带有4个按钮的视图。我有一个相对的布局来正确放置它们。问题是,我希望按钮能够填充整个屏幕而不会消失,我不希望为它们设置固定的大小,以确保按钮能够适应任何屏幕大小。现在我唯一能看到的是一个大按钮,因为RelativeLayout没有android:layout\u weight。我怎样才能做到这一点?以下是我得到的: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/search_icon"
android:background="@drawable/search_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/categories_icon"
android:background="@drawable/categories"
android:layout_toRightOf="@+id/search_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/green_icon"
android:background="@drawable/green_icon"
android:layout_below="@+id/search_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/red_icon"
android:background="@drawable/red_icon"
android:layout_toRightOf="@+id/green_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
关于RelativeLayout的问题是,你必须知道把iTen放在哪里。例如:
<Button android:id="@+id/buyTicket"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Buy ticket"
android:layout_above="@id/LinearLayout02" />
<ImageView android:id="@+id/logo"
android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/buyTicket" />
请注意android:layout_over=“@id/buyTicket”。
如果您希望按钮A位于按钮B的顶部,则首先声明按钮A(例如),将其与父按钮的顶部对齐,然后在按钮B声明处,您说希望它位于按钮A下
在您的情况下,您必须声明按钮A、B、C和D,然后在按钮A中设置
android:layout\u alignParentTop=“true”
,然后在B、C和D上设置android:layout\u bellow
(设置到它们前面的位置)。别忘了将高度和宽度设置为wrap\u content
这就是你要找的吗?
以下是我为该布局所做的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="BUTTON 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:text="BUTTON 2"
android:layout_toRightOf="@+id/search_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="BUTTON 3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<Button
android:text="BUTTON 4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
嗯,看起来比我想象的要容易。。。我想你正在寻找这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_>
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" />
<Button android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3" />
<Button android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
试试看,应该是这样的:
如果您有以下问题,请阅读这家伙的博客:
有很多例子,对重量等问题解释得很好。如果您在“填充父项”上设置了所有内容,那么整个屏幕上只有4个按钮。这是对上述问题的回答。一切都设置为填充父对象。我只能看到2个第一个按钮,如果我没有在按钮中设置图像作为背景,它就可以工作。否则,我只能看到前两个按钮,只是为了弄清楚……所以,你让四个按钮占据整个屏幕,然后将android:background属性添加到每个图像中,只会显示前两个按钮?是的,我为这四个按钮设置了背景,因为我想为按钮设置一个图标,以获得更好的外观。当我将图像设置为按钮的背景时,我只能看到两个按钮。但是如果没有图标,它会工作。很好,它工作了,有什么诀窍?图标变形了,但我想我应该获得屏幕大小,并以编程方式放置图标,不是吗?谢谢你的回答!!没有诡计,只有尝试和错误。。。Android界面有点棘手,有时你只需要测试各种布局就可以看到最好的。关于图标,我帮不了你。。。我还没用过。但是,就像布局一样,你需要反复尝试。好的,谢谢你花时间来帮助。我真的很感激。祝您有个美好的一天。