[英]Distributing android vertical SeekBars evenly across parent
我用下面的代碼創建了一個“標准”垂直SeekBar(在SO上多次發布)。
package com.plainfieldworks.nofiseq;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class VerticalSeekBar extends SeekBar{
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public VerticalSeekBar(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
現在,我想使用其中的6個。 如果我只是使用RelativeLayout將它們彼此相鄰放置,那么一切都很好(無法發布圖片,缺乏聲譽...)。
但是,我希望它們在水平方向上均勻分布在整個視圖中,如果我在布局XML中將它們添加為LinearLayout的子代,並將它們的權重設置為1,則progressDrawable(我認為是-“欄”)將填充整個視圖寬度稍加填充,拇指將放在左側。
現在,我明白了為什么會發生這種情況,但是我不知道如何解決。 即我如何保持默認比例,並仍將SeekBars在整個視圖中均勻地分布在每個視圖中,以progressDrawable和thumb為中心?
一般來說,我是android和Java開發的新手,所以請耐心等待。 我希望我能解釋這個問題。
使我胖乎乎的SeekBars的XML布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/cream"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".EditInst"
android:padding="2dp">
<LinearLayout android:id="@+id/header"
android:background="#ffffbc40"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView android:id="@+id/instTxt"
android:textIsSelectable="false"
android:textColor="#ff000000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
<LinearLayout android:id="@+id/SeekBars"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/header">
<com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/f0SeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
<com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/f1SeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
<com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/f2SeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
<com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/cSeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
<com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/sSeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
<com.plainfieldworks.nofiseq.VerticalSeekBar android:id="@+id/dSeekBar" android:padding="10dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"/>
</LinearLayout>
</RelativeLayout>
最直接的方法可能只是在兩者之間添加另一個層。 您可以使用諸如當前當前SeekBar所在的FrameLayout
類的東西,這樣您就可以擁有“胖的FrameLayout
”,然后在每個FrameLayout
放置一個SeekBar。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.