Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓设置按钮宽度为屏幕的一半_Android_Xml_Layout - Fatal编程技术网

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

Android 安卓设置按钮宽度为屏幕的一半,android,xml,layout,Android,Xml,Layout,如何设置(XML)按钮宽度为屏幕的一半。我只找到了包装内容、匹配父项(填充整个屏幕)和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_h

如何设置(XML)按钮宽度为屏幕的一半。我只找到了包装内容、匹配父项(填充整个屏幕)和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" />


这可以通过在布局上有两个小部件来实现: 使用线性布局,在两个小部件(按钮和另一个小部件)上设置
layout\u width=“fill\u parent”
,并将layout\u 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" />

这在XML中是不可能的。但是,您可以在Java中通过使用获取显示的宽度,将其除以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.
使用文本视图

<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

很棒。简单而聪明。非常感谢。xml最好的解决方案!非常感谢。这是我以后需要的东西。