[英]How to add seekbar to custom dialog without XML
此類是我在應用程序中調用的對話框,它的工作原理很好,它的中心有一個大黑點,該對話框不需要XML文件。 我想向此對話框添加一個搜索欄,因為我想在滾動搜索欄時更改剛才提到的點的大小。
public class StrokePicker extends Dialog {
StrokePicker strokepicker;
public StrokePicker(Context context) {
super(context);
}
public class StrokePickerView extends View {
Paint Stroke;
SeekBar seekbar;
private static final int mCENTER= 32;
public StrokePickerView(Context context) {
super(context);
Stroke = new Paint(Paint.ANTI_ALIAS_FLAG);
Stroke.setStyle(Paint.Style.FILL);
Stroke.setColor(Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawCircle(canvas.getWidth()/2-mCENTER, canvas.getHeight()/2-mCENTER*3, mCENTER, Stroke);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(125 * 2, 100 * 2);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(getContext());
SeekBar seekbar;
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER);
layout.setPadding(10, 10, 10, 10);
layout.addView(new StrokePickerView(getContext()),
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
setContentView(layout);
setTitle("Grosor");
}
}
我沒有找到有關如何在不使用XML的情況下添加搜索欄的信息,我們將不勝感激。 再見,謝謝。
嘗試創建一個SeekBarPreference
要么
builder.setView(View v); here is how you can use it.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
SeekBar sb = (SeekBar)layout.findViewById(R.id.yourSeekBar);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
//Do something here with new value
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.