SeekBar背景在启动时移位

时间:2019-01-03 12:08:47

标签: java android android-studio android-layout

我在布局中使用了多个搜索栏。我面临的问题是,当应用程序第一次打开时,第一搜索栏的背景总是被替换。如果应用是从后台打开的,则搜索栏会正确显示。请帮忙。谢谢!

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

List<seek> seeklist = new ArrayList<seek>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);

    getSupportActionBar().hide();
    RelativeLayout rlStart = new RelativeLayout(this);

    seeklist.add(new seek(50, 700, 600, 60));
    seeklist.add(new seek(200, 700, 600, 60));
    seeklist.add(new seek(350, 700, 600, 60));
    seeklist.add(new seek(500, 700, 600, 60));
    seeklist.add(new seek(50, 1400, 600, 60));
    seeklist.add(new seek(200, 1400, 600, 60));
    seeklist.add(new seek(350, 1400, 600, 60));
    seeklist.add(new seek(500, 1400, 600, 60));


    for (int i = 0; i < seeklist.size(); i++) {

        SeekBar seekBar = getWidgetSeekBar(
                seeklist.get(i).getLeft(), seeklist.get(i).getTop(), 
seeklist.get(i).getWidth(), seeklist.get(i).getHeight());

        rlStart.addView(seekBar);

    }


    setContentView(rlStart);
}

public SeekBar getWidgetSeekBar(int left, int top, int width, int 
height) {

    SeekBar seekbar = (SeekBar) 
LayoutInflater.from(this).inflate(R.layout.layout_seekbar, 
null).findViewById(R.id.seekBar);

    RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
            width, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp1.leftMargin = left;
    lp1.topMargin = top;

    seekbar.setThumbOffset(8);
    seekbar.setSplitTrack(false);


    LayerDrawable layer = (LayerDrawable)seekbar.getProgressDrawable();

    Bitmap bmp = 
    BitmapFactory.decodeResource(getResources(),R.drawable.seekbar_bg);

    layer.setDrawableByLayerId(android.R.id.background, new 
    BitmapDrawable(getResources(), bmp));
    seekbar.setMax(1000);
    seekbar.setLayoutParams(lp1);
    seekbar.setRotation(270f);

    return seekbar;
}

}

layout_seekbar.xml与

<?xml version="1.0" encoding="utf-8"?>
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/seekBar"
android:maxHeight="1000dip" >

当应用程序第一次打开(总是从后台删除)时,会发生此问题。如果它是从后台打开的,则问题不会持续。

Attached image for reference

0 个答案:

没有答案