LinearLayout中的圆形按钮:垂直 [英] Circular Buttons in LinearLayout:Vertical
问题描述
我创建了自己的可绘制对象,将其设置为图像按钮的背景. (在此处所述)
I have created my own drawable object, which I am setting to be the background of an image button. (described 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来实现这一点. (在此处描述)
I want these to be spaced equally vertically down the side of the screen, so I want to use android:layout_weight to achieve this. (Described 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.
What ever I do I cannot seem to have them spaced out equally on any screen size let alone scaled for each of them. I think my issue is that I have to specify height & width to form the circle, but as soon as I do it messes with the layout_weight as its been (for want of a better term) 'hardcoded'.
我假设可以有6个圆形按钮的垂直布局,它们在屏幕上等距分布,但是我不能算出怎么做?我也尝试过不使用<Space>
,而是在<Button>
中添加了填充.
I assume it is possible to have a vertical layout of 6 circular buttons, spaced equally down the screen but I cannot work out how? I have also tried without the <Space>
, and added padding to the <Button>
instead.
推荐答案
我遇到了同样的问题.我必须在等距的水平LinearLayout中有4个圆形按钮.
I had the same problem. I had to have 4 circular buttons in a horizontal LinearLayout equally spaced.
<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内
And to make them all of the same height I placed this code inside onCreate on my Activity
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());
}
});
希望这可以帮助遇到相同问题的人.
Hope this helps someone with the same problem.
这篇关于LinearLayout中的圆形按钮:垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!