【问题标题】:Android setting Button width half the screenAndroid设置按钮宽度为屏幕的一半
【发布时间】:2012-07-01 22:35:14
【问题描述】:

如何设置(在 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" />

【问题讨论】:

    标签: android xml layout


    【解决方案1】:

    这可以通过在您的布局中添加两个小部件来完成: 使用 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" />
    

    【讨论】:

    • 太棒了。简单而聪明。谢谢!
    • 最好的 xml 解决方案!
    【解决方案2】:

    这在 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.
    

    【讨论】:

    • 谢谢。那是我稍后需要的东西。
    【解决方案3】:

    使用文本视图。

    <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" />
    

    找到答案here

    我在堆栈上的第一篇文章,希望对我有所帮助 XD

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-17
      • 2013-04-17
      • 1970-01-01
      • 2013-08-05
      • 2019-05-19
      • 2012-03-17
      • 1970-01-01
      • 2016-05-18
      相关资源
      最近更新 更多