GVKun编程网logo

android – addView和addViewInLayout有什么区别(安卓addview)

12

本文将介绍android–addView和addViewInLayout有什么区别的详细情况,特别是关于安卓addview的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题

本文将介绍android – addView和addViewInLayout有什么区别的详细情况,特别是关于安卓addview的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于addView方法后Android视图未显示、Android AddView() 多个 导致 加载非常缓慢。、Android DataBinding动态addView、Android findViewById与findViewWithTag()的知识。

本文目录一览:

android – addView和addViewInLayout有什么区别(安卓addview)

android – addView和addViewInLayout有什么区别(安卓addview)

我见过一些使用addView的小部件,有时还有addViewInLayout.

它们之间有什么区别?
如果我用另一个替换一个会发生什么?

我应该在布局期间保留一个标志并相应地使用“addViewInLayout”或“addView”吗?

谢谢.

BR,
亨利

PS.添加更多标签:removeview,removeviewinlayout

解决方法

在onLayout中调用addView通常是一个坏主意,因为addView在内部触发了一个最终将调用onLayout的requestLayout.因此,当您处于布局中间时,最终会触发布局.

如果您真的需要在onLayout中添加新视图,addViewInLayout是addView的“更安全”版本.它基本上不会触发布局传递(不会在内部调用requestLayout).

有关更多详细信息,请参阅此视频(由android工程师):http://www.youtube.com/watch?v=HbAeTGoKG6k

addView方法后Android视图未显示

addView方法后Android视图未显示

我有ViewGroup.addView()方法的问题.我使用此代码为我的布局添加新视图:

TalbeLayout parent = (TableLayout)findViewById(R.id.this_does_not_matter);
parent.removeAllViews(); //this view group contains something at start
TextView tv = new TextView(this);
tv.setText("some text");
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
parent.addView(tv);

之后,我无法看到我的TextView.更重要的是,parent.getChildCount()返回正确的值(如果我尝试添加一个子节点,则返回1).在父视图的onClick()方法中,我尝试获取TextView的宽度和高度,所有这些都等于0.为TableView调用requestLayout(),invalidate()和measure(500,50)并且没有效果.我甚至尝试在view.post(Runnable)的帮助下添加新视图,尽管此代码在UI线程中执行.

我很困惑.我真的不明白会发生什么.有人可以解释一下我做错了什么吗?

一个有趣的时刻:
setLayoutParams()无效.如果我设置宽度= 500和高度= 50的参数,在onClick方法中我得到宽度= -1和高度= -1的参数.

TableRow添加后的代码:

TableLayout parent = (TalleLayout)findViewById(R.id.this_does_not_matter);
parent.removeAllViews(); //this view group contains something at start
TextView tv = new TextView(this);
tv.setText("some text");
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(500,50);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(500,50);
TableRow tr = new TableRow(this);
tr.addView(tv,tlp);
parent.addView(tr,lp);
parent.invalidate();
parent.requestLayout();

我发现了一件有趣的事情.此活动从TabHost运行.只有在此TabHost中首次选择“活动”时,才会显示“addView bug”.如果我第一次从另一个标签启动Activity,一切正常.

解决方法

我找到了解决方案如果关闭布局动画与android:animateLayoutChanges =“false”一切正常.但我仍然不知道为什么会这样.如果有人知道这种行为的原因,那对我来说很有趣.

Android AddView() 多个 导致 加载非常缓慢。

Android AddView() 多个 导致 加载非常缓慢。

如题,有很多地方 要根据 后台给的数据动态添加 View 。 每次加载都会卡住,很慢

Android DataBinding动态addView

Android DataBinding动态addView

我有两个布局xml A和B.

带有id’layout’的A xml中的linearlayout

现在我想使用layout.addView()在布局中添加B.

如何通过使用数据绑定来完成此操作

解决方法

我不认为这是最好的做法,但这是我用数据绑定动态添加视图的方法.

在布局A中,我有一个如下的FrameLayout:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    bind:createView="@{viewmodel.dynamicViews}">

在我的viewmodel类中,我有一个带有BindingAdapter注释的静态方法,

@BindingAdapter("bind:createView")
public static void createImproveView(FrameLayout layout,LinearLayout replacement) {
    layout.removeAllViews();
    layout.addView(replacement);
}

我在这里有替换布局:

public LinearLayout getDynamicViews() {
    LinearLayout layout = new LinearLayout(mContext);
    // dynamically add views here. This Could be your layout B.
    return layout;
}

我找不到任何其他解决方案,这对我来说很好.请给我任何评论,我愿意学习更好的解决方案!

Android findViewById与findViewWithTag()

Android findViewById与findViewWithTag()

一、方法介绍

android 平台获取控件的方式,一般使用View.findViewById(int)但还有一个方findViewWithTag(Object key),indViewById(int resId)适合正向匹配查询,而findViewWithTag(Object key)适合反向查询。但2者中,后者功能比较弱,不能查询到所有的具有该tag的View。

在这里,着重讲解findViewWithTag

实际操作,往往是对固定的tabcard,或者TabWidget来说非常合适,另外有些时候,对于没必要设置ViewHolder的控件来说也非常合适。
在自定义tabWidget时,饿哦们可以将每个widget的tab设置成tag,然后读取的方式非常好,当然,这功能可有可无。

二、代码实现

1.布局文件R.layout.main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical">  
      
    <!-- 这个布局决定了标签在上面还是在下面显示 -->  
    <FrameLayout   
        android:id="@+id/realtabcontent"  
        android:layout_width="match_parent"  
        android:layout_height="0dip"  
        android:layout_weight="1" />  
      
    <android.support.v4.app.FragmentTabHost  
        android:id="@android:id/tabhost"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content">  
          
        <TabWidget   
            android:id="@android:id/tabs"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:orientation="horizontal"/>  
    </android.support.v4.app.FragmentTabHost>  
      
</LinearLayout>

2.java代码

public class MainActivity extends FragmentActivity {  
  
    private FragmentTabHost mTabHost = null;;  
    private View indicator = null;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
  
        setContentView(R.layout.main);  
  
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);  
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
  
        // 添加tab名称和图标  
        indicator = getIndicatorView("我的联系人", R.layout.mycontact_indicator);  
        mTabHost.addTab(mTabHost.newTabSpec("myContact")  
                .setIndicator(indicator), FirstFragment.class, null);  
  
        indicator = getIndicatorView("陌生人", R.layout.strangercontact_indicator);  
        mTabHost.addTab(  
                mTabHost.newTabSpec("stranger").setIndicator(indicator),  
                secondFragment.class, null);  
  
        indicator = getIndicatorView("常联系人", R.layout.alwayscontact_indicator);  
        mTabHost.addTab(  
                mTabHost.newTabSpec("alwaysContact").setIndicator(indicator),  
                ThirdFragment.class, null);  
    }  
  
    private View getIndicatorView(String name, int layoutId)
     {  
        View v = getLayoutInflater().inflate(layoutId, null);  
        TextView tv = (TextView) v.findViewById(R.id.tabText);  
        tv.setText(name);  
        
        v.setTag(name);  //设置tag
        
        return v;  
    }  
  
    @Override  
    protected void onDestroy() {  
        // TODO Auto-generated method stub  
        super.onDestroy();  
        mTabHost = null;  
    }  
}
alwayscontact_indicator.xml文件

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:layout_gravity="center"   
    android:gravity="center">  
  
    <TextView   
        android:id="@+id/tabText"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:focusable="true"  
        android:drawableTop="@drawable/mycontact_selector"  
        android:textColor="@drawable/tabitem_txt_sel"/>  
</LinearLayout>

 

3.当我们需要命令模式(是一种设计模式,类似于BroadcastReciever的Action进行Tab切换)先来建立一个内部类

private BroadcastReceiver commandBroadcast = new BroadcastReceiver()
{

    private final String commondKey = "COMMOND_KEY";
    @override
    public void OnReceiver(Context  cxt,Intent data)
    {
       if("我的联系人".equals(intent.getStringExtra(commondKey)))
       {
           mTabHost.setCurrentTag("myContact");
           View layouView = mTabHost.getTabWidget().findViewWithTag("我的联系人");
       }
       else if("陌生人".equals(intent.getStringExtra(commondKey)))
       {
            mTabHost.setCurrentTag("stranger");
            View layouView = mTabHost.getTabWidget().findViewWithTag("陌生人");
       }
    }

}

4.在Activity中注册commandBroadcast,Action使用(具体步骤略过)

cn.test.my.tab.action

5.当我们需要切换时tabcard时,直接可以发送广播

Intent intent = new Intent("cn.test.my.tab.action");
intent.putString("COMMOND_KEY","我的联系人");
sendBroadcast(intent);

 

就到这里,如有疑问,请留言

 

try doing it!

 

我们今天的关于android – addView和addViewInLayout有什么区别安卓addview的分享就到这里,谢谢您的阅读,如果想了解更多关于addView方法后Android视图未显示、Android AddView() 多个 导致 加载非常缓慢。、Android DataBinding动态addView、Android findViewById与findViewWithTag()的相关信息,可以在本站进行搜索。

本文标签: