Android设置按钮宽度为屏幕的一半。

4
如何在XML中设置按钮宽度为屏幕的一半?我只找到了wrap content(内容自适应大小)、match parent(填充整个屏幕)和确切的dp值,比如50dp。如何让它精确地占据一半屏幕的宽度?
<RelativeLayout 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:weightSum="2"
>

 <Button
     android:id="@+id/buttonCollect"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:paddingLeft="8dp"
     android:paddingRight="8dp"
     android:text="przycisk" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Button" />

3个回答

14

可以通过在布局中有两个小部件来实现这一点: 使用LinearLayout,并在两个小部件(按钮和另一个小部件)上设置layout_width="fill_parent",并将layout_weight也设置为相同的值。 LinearLayout将平均分配宽度给两个小部件,你的按钮将占据屏幕的一半。

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

 <Button
     android:id="@+id/buttonCollect"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:paddingLeft="8dp"
     android:paddingRight="8dp"
     android:text="przycisk" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Button" />

太棒了。简单而聪明。谢谢! - Yoda
XML最好的解决方案! - Sonhja

8

这在XML中是不可能的。然而,在Java中,您可以通过使用DisplayMetrics获取显示器的宽度,将其除以2并将其设置为按钮的宽度来实现。类似于这样:

Button button = (Button) findViewById(R.id.button);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int buttonWidth = width/2;
//Apply this to your button using the LayoutParams for whichever layout you have.

谢谢。那是我稍后需要的东西。 - Yoda

0
使用TextView。
<TextView
    android:id="@+id/textViewHelper"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

然后将这两个按钮中的一个或者全部添加到它的左侧和/或右侧。

<Button
    android:id="@+id/saveList"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/deleteAllPlaylists"
    android:layout_toRightOf="@+id/textViewHelper"
    android:text="@string/save_temp_playlist" />

<Button
    android:id="@+id/deleteAllPlaylists"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/textViewHelper"    
    android:text="@string/delete_all_playlists" />

答案可以在这里找到。

我在Stack上的第一篇帖子,希望能有所帮助 XD


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接