如何通过Android中的视图编辑布局?

时间:2017-06-23 08:48:19

标签: android android-layout twitter view

在我的项目中,我可以访问要编辑的View,但我没有其布局XML文件。我知道我可以通过自定义布局来自定义替换View,但我更喜欢编辑它并尝试在其左侧添加一个简单的TextView

这就是我访问View的方式:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    return view;
}

这是我可以得到的View

enter image description here

这就是我要编辑的内容:

enter image description here

如何访问其布局,或如何编辑它?

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用getView()中的以下方法:

private  View addTextView(View view){

    int TEXT_VIEW_ID=2;
    RelativeLayout relativeLayout = new RelativeLayout(view.getContext());

    TextView textView = new TextView(relativeLayout.getContext());
    textView.setId(TEXT_VIEW_ID);
    RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START|RelativeLayout.ALIGN_PARENT_LEFT);
    textView.setLayoutParams(layoutParams);
    relativeLayout.addView(textView);


    RelativeLayout.LayoutParams layoutParams1= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams1.addRule(TEXT_VIEW_ID,RelativeLayout.RIGHT_OF);
    view.setLayoutParams(layoutParams1);
    relativeLayout.addView(view);
    return relativeLayout;
}

更改您的getView()方法,如下面的代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
// add method here like below line
    return addTextView(view);
}