管理相对布局儿童的可见性

时间:2013-11-26 13:09:55

标签: android relativelayout

我必须在点击其他视图时实现扩展/折叠(显示/隐藏)特定视图。最终让它工作但是有一个小问题。
我有一个有两个孩子的相对布局:第一个是textview,第二个是线性布局 在textview的click事件中,我设置了以下线性布局的可见性(VISIBLE或GONE) 现在的问题是,在线性布局可见后,它会以某种方式设法隐藏文本视图 我尝试了textview.bringToFront(),但它只是使textview与linearlayout的第一行重叠,即textview位于线性布局内容之上。
我尝试将textview放在线性布局中,但没有区别 我尝试将线性布局设置为BELOW。一切都是徒劳的..
我知道textview存在,因为当我点击第一行(与textview重叠)时,click事件被触发了。
我想要的是,无论我的textview应该是什么,并且如果可见,线性布局必须在textview下面的位置。
编辑

RelativeLayout wrapperlayout = new RelativeLayout(getActivity());
//wrapperlayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));

//-------------HANDLE---------------------------
TextView txtHeader = new TextView(getActivity());
txtHeader.setOnClickListener(new OnClickListener() {

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        TextView tv = (TextView) v;
        RelativeLayout rParent =  (RelativeLayout) tv.getParent();
        LinearLayout lInner = (LinearLayout) rParent.getChildAt(1);

        if(lInner.isShown())
        {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
            //tv.bringToFront();
            lInner.setVisibility(View.GONE);
            //lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up));
        }
        else{
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.collapse, 0, 0, 0);
            //tv.bringToFront();
            lInner.setVisibility(View.VISIBLE);
            lInner.setTop(tv.getBottom());
            //lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down));
        }


    }
});
txtHeader.setText("Header");
txtHeader.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
txtHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));


//--------------CONTENT-------------------------
LinearLayout lContent = new LinearLayout(getActivity());
lContent.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lContent.setOrientation(LinearLayout.VERTICAL);

HashMap<String, String> MySet = new HashMap<String, String>();
MySet = getData();
Iterator<String>RowItr = MySet.keySet().iterator();
int rowcnt = 0;
while (RowItr.hasNext()) {
    LinearLayout lRow = new LinearLayout(getActivity());
    LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    lRow.setLayoutParams(lparams1);
    lRow.setOrientation(LinearLayout.HORIZONTAL);

    TextView txtLbl = new TextView(getActivity());
    txtLbl.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
    TextView txtVal = new TextView(getActivity());
    txtVal.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));

    String Lbl = RowItr.next();
    txtLbl.setText(Lbl);
    if(rowcnt % 2 != 0)
        lRow.setBackgroundColor(Color.parseColor("#dbe4f0"));
    else
        lRow.setBackgroundColor(Color.parseColor("#ffffff"));
    txtVal.setText(MySet.get(Lbl));
    lRow.addView(txtLbl);
    lRow.addView(txtVal);
    lRow.setPadding(10, 10, 10, 10);

    lContent.addView(lRow);
    rowcnt++;
}                       
lContent.setVisibility(View.GONE);

wrapperlayout.addView(txtHeader);
wrapperlayout.addView(lContent);
RelativeLayout.LayoutParams rPARAMS = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//rPARAMS.addRule(RelativeLayout.ABOVE, txtHeader.getId());
//rPARAMS.addRule(RelativeLayout.BELOW, lContent.getId());
wrapperlayout.setLayoutParams(rPARAMS);

2 个答案:

答案 0 :(得分:0)

你可以将LinearLayout存储为实例变量,只需调用layout.setvisibility(View.GONE);在你的onClick方法中。怀疑任何其他解决方案,除非你想保存父(你的两个视图附加到的布局)并在onClick内执行findViewById或调用getChildAt(1)orso

答案 1 :(得分:0)

有一个解决方案..我尝试设置边距..而且一切都很好.. te句柄和内容都可见而不隐藏任何其他视图..