自定义视图重叠

时间:2017-08-24 18:05:06

标签: android relativelayout

我定义了我膨胀的布局行,并将实际视图添加到线性布局。

我想要这样的事,

https://i.stack.imgur.com/EKPmJ.png

这是我主要活动的xml,我正在给服装视图行充气

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.skw.customeviewdemo.MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/inflate"
    android:text="inflate"/>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list_container">

    </LinearLayout>
</LinearLayout>

这是我正在膨胀的行的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="its text view"
        />

    <Button
        android:id="@+id/nameButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        />
    <ImageView
        android:layout_marginTop="-25dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>
</RelativeLayout>

这是我的java代码

public class MainActivity extends AppCompatActivity {

    Button b;
    LinearLayout l1;
    Context context;
    Boolean isViewCreated = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String Namearray[] = {"sagar", "ranjeet", "akash", "kate"};
        this.context = this;

        b = (Button) findViewById(R.id.inflate);
        l1 = (LinearLayout) findViewById(R.id.list_container);

        createViews(Namearray);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(l1.getVisibility() == View.VISIBLE && isViewCreated)
                {
                    l1.setVisibility(View.GONE);
                }
                else
                {
                    l1.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    void createViews(final String[] namearray)
    {
        for(int i=0;i < namearray.length;i++){
            final int j = i;
            View view = LayoutInflater.from(context).inflate(R.layout.layout_item,null);
            TextView button1 = (TextView) view.findViewById(R.id.name);
            Button button2 = (Button) view.findViewById(R.id.nameButton);


            button1.setText("HELLO " + namearray[i]);
            button2.setText("Click to know my name ");

            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"Hello " + namearray[j],Toast.LENGTH_LONG).show();
                }
            });
            view.setId(generateViewId());

            try {
                l1.addView(view);
                isViewCreated = true;
            }
            catch (Exception e)
            {

            }
        }
        l1.setVisibility(View.GONE);
    }

    private static final AtomicInteger viewIdGenerator = new AtomicInteger(15000000);

    public static int generateViewId() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return generateUniqueViewId();
        } else {
            return View.generateViewId();
        }
    }

    private static int generateUniqueViewId() {
        while (true) {
            final int result = viewIdGenerator.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (viewIdGenerator.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }
}

但是当我给行充气时,我的图像视图得到截止,我不希望我尝试使用负边距,但它不起作用 这就是它的样子 custom overlapping row

我做了什么,所以我将图像叠加到上方视图上?

1 个答案:

答案 0 :(得分:0)

尝试将布局更改为此。

var input = $('#input');
var response = null;
var dataGlobal = {};

function ajax(query) {//ajax now only takes one variable  
  var start = 2;

  if (query.length >= start) {
    if (response != null)
      response.abort();

    response = $.ajax({
      url: 'https://cors-anywhere.herokuapp.com///en.wikipedia.org/w/api.php?action=opensearch&format=json&limit=8&suggest&search=' + query,
      type: 'GET',
      data: {},
      dataType: 'json',
      success: function(data) {
        console.log(data);
        dataGlobal = data;
        searchSuggest(dataGlobal);
      }
    });
  }
}

当您放入另一行时,需要将该行向上推,以便现在存在的android图标覆盖它。