【问题标题】:Circular Buttons in LinearLayout:VerticalLinearLayout 中的圆形按钮:垂直
【发布时间】:2013-09-14 02:27:47
【问题描述】:

我创建了自己的可绘制对象,我将其设置为图像按钮的背景。 (描述here

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <solid android:color="#A7BF1D"/> </shape>

我希望它们在屏幕的一侧垂直等距分布,所以我想使用 android:layout_weight 来实现这一点。 (描述为here

<LinearLayout 
        android:id="@+id/left_bar"
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="8">
        <Button
            android:id="@+id/btnCall"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"
            android:background="@drawable/btn_circle"            
            android:text="Call"
            android:textSize="12sp" />
       <Space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"></Space>
        <Button
            android:id="@+id/btnSpeak"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"
            android:background="@drawable/btn_circle"
            android:text="Speak"
            android:textSize="12sp" />
       <Space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"></Space>
        <Button
            android:id="@+id/btnRecordedMessage"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"
            android:background="@drawable/btn_circle"
            android:text="Recorded Message"
            android:textSize="12sp" />
        <Space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"></Space>
        <Button
            android:id="@+id/btnSelectAll"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"
            android:background="@drawable/btn_circle"
            android:text="Select All Units"
            android:textSize="12sp" />
       <Space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"></Space>
        <Button
            android:id="@+id/btnDeselectUnits"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"
            android:background="@drawable/btn_circle"
            android:text="Deselect Units"
            android:textSize="12sp" />
        <Space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"></Space>
        <Button
            android:id="@+id/btnAnswer"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="1"
            android:background="@drawable/btn_circle"
            android:text="Answer"
            android:textSize="12sp" />
    </LinearLayout>

无论我做什么,我似乎都无法让它们在任何屏幕尺寸上均匀分布,更不用说为它们中的每一个缩放了。我认为我的问题是我必须指定高度和宽度来形成圆圈,但是一旦我这样做,它就会与 layout_weight 一样(因为需要更好的术语)“硬编码”。

我假设可以有 6 个圆形按钮的垂直布局,在屏幕上等距分布,但我不知道怎么做?我也试过不使用&lt;Space&gt;,而是在&lt;Button&gt; 中添加了填充。

【问题讨论】:

  • 无论屏幕尺寸如何,您都想为这些按钮设置一个圆圈吗?

标签: android android-layout android-linearlayout


【解决方案1】:

嗯。一方面,LinearLayouts 子项中的所有 layout_widths 都设置为 match_parent。在第一个孩子渲染之后,就没有其他空间了,因为它会占用所有空间。

其次,我强烈建议不要使用所有标签。相反,我只会为您的按钮设置填充(或边距)。

【讨论】:

  • 'View.Button' 对象的任何一侧都不需要空间,我希望它们填充包含它们的 LinearLayout 的宽度,并且高度等于宽度使@drawable/btn_circle 是圆形的,而不是我现在得到的椭圆形。使它们成为圆形的唯一方法是设置高度和宽度,但随后它们会聚集在视图的顶部,并且不会沿其长度均匀分布。
  • 方向是垂直的,所以宽度无关
  • @codeMagic。谢谢。我错过了这一点,这改变了一切。
【解决方案2】:

如果我理解正确,您希望LinearLayout 中的Views 与weight 隔开。如果是这样,那么android:layout_weight 应该在每个View 上,而不是LinearLayoutLinearLayout 将具有 android:weightSum 并且每个 View 将具有说 1 的 weight。但是如果您希望它们全部均匀分布,您甚至不需要 weightSum

<LinearLayout 
    android:id="@+id/left_bar"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_weight="8">  // Try removing this and add weights to your views

我不知道这是否能解决您的问题,但我会从那开始

【讨论】:

  • android:layout_weight="8" 那条线用来定义 LinearLayout 的宽度,我有一个 left_bar、中间 ImageMap 和 right_bar。基本上,最终视图是中间的 Map 对象,两侧都有控件。它用于管理应用程序,控件不是用于 android 组件(缩放滚动等),而是用于应用程序控制的设备。
  • 所以这个LinearLayout 在另一个里面?如果是这样,那么它的高度或宽度应该是 0dp,具体取决于父 `LinearLayout 的方向
  • 确实如此,它在另一个LinearLayout android:orientation="horizo​​ntal"中。这有 3 个子视图,左/右栏和地图。
  • 那么它的width 应该是0dp。另外,所有按钮的宽度都是0dp的高度吗?
  • 目前水平布局中的所有 3 个子项在高度和宽度上都设置为 fill_parent。重量使它们的宽度正确。
【解决方案3】:

我遇到了同样的问题。我必须在等距的水平 LinearLayout 中有 4 个圆形按钮。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="10dp" >

        <Button
            android:id="@+id/btn1"
            style="@style/BotonCircular"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="@drawable/boton_circulo_negro" />
        <View 
            android:layout_weight="0.033"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />

        <Button
            android:id="@+id/btn2"
            style="@style/BotonCircular"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="@drawable/boton_circulo_negro" />
        <View 
            android:layout_weight="0.033"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />
        <Button
            android:id="@+id/btn3"
            style="@style/BotonCircular"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="@drawable/boton_circulo_negro" />
        <View 
            android:layout_weight="0.033"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />
        <Button
            android:id="@+id/btn4"
            style="@style/BotonCircular"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="@drawable/boton_circulo_negro" />

    </LinearLayout>

为了使它们都具有相同的高度,我将此代码放在我的 Activity 上的 onCreate 中

final Button btn1 = (Button) findViewById(R.id.btn1);
final Button btn2 = (Button) findViewById(R.id.btn2);
final Button btn3 = (Button) findViewById(R.id.btn3);
final Button btn4 = (Button) findViewById(R.id.btn4);
ViewTreeObserver vto = btn1.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        btn1.setHeight(btn1.getWidth()); 
        btn2.setHeight(btn1.getWidth());    
        btn3.setHeight(btn1.getWidth());
        btn4.setHeight(btn1.getWidth());
    }
});

希望这可以帮助遇到同样问题的人。

【讨论】: