动态添加视图而不显示视图

问题描述 投票:5回答:4

我正在使用以下代码在我的布局的子级LinearLayout下为视图添加一个视图:

LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
 textView.setText("your text");
// insert into main view
View insertPoint = findViewById(R.id.insert_point);
((ViewGroup) insertPoint).addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

但是我看不到那里的景色。当我将屏幕旋转到横向视图时,它会显示出来。如何解决这个问题?

编辑:

基本上是我在做什么。我有一个带有两个标签的TabActivity。我使用全局应用程序,该应用程序存储我的TabActivity的标志和TabHost。我还在第二个选项卡活动中使用一个线程,该线程正在连续监视应用程序全局以监视标志。单击第一个选项卡中的按钮时,将全局设置为true。第二个选项卡中的线程检查其值。当它变为真时,我运行异步任务,并使用ApplicationGlobal中搅动的TabHost将当前的显示窗口移至第二个选项卡。

第二个标签的代码:

public class ShowDownloadsActivity extends Activity {
    private List<String> list;
    ApplicationGlobal ap;
    String tempUrl = "";
    LinearLayout lly;
    LayoutInflater inflater;
    Context con;
    static int k = 0;
    static int tmp = 0;
    static int size = 0;
    View insertPoint;
    View v;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shwds);
        con = this;
        v = LayoutInflater.from(this).inflate(R.layout.downloadlistrow, null);
        insertPoint = (View) findViewById(R.id.layout_vids);
        inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ap = (ApplicationGlobal) getApplication();
        lly = (LinearLayout) findViewById(R.id.layout_vids);

        Thread thread1 = new Thread() {
            public void run() {
                try {
                    boolean flg = ap.getFlag();
                    if (flg) {
                        tempUrl = ap.getUrl();
                        ap.setUrl("");
                        flg = false;
                        new downloadVideo().execute();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        thread1.run();
    }

    class downloadVideo extends AsyncTask<Hashtable<String, String>, String, String> {
        String resp = "";

        protected void onProgressUpdate(String... values) {

        }

        @Override
        protected void onPreExecute() {
            // fill in any details dynamically here 
            TextView textView = (TextView) v.findViewById(R.id.siz);
            textView.setText("your text");
            // insert into main view 
            addContentView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Hashtable<String, String>... prmss) {
            try {
                final int return resp;
            }

            @Override
            protected void onPostExecute (String result){
                super.onPostExecute(result);
            }
        }
    }
}
android view visibility
4个回答
3
投票

首先初始化要向其添加子视图的布局。

例如

LinearLayout layout=(LinearLayout)findViewById(R.id.your_root_layout);

然后像下面那样膨胀您的布局

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View v= (View) mInflater.inflate(R.layout.your_layout, null);
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView, p);

希望这会对您有所帮助。


0
投票
// try this
**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="wrap_content"
    android:padding="5dp"
    android:orientation="vertical">
</LinearLayout>

**your_layout.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/a_text_view"/>
</LinearLayout>

**MyActivity**
public class MyActivity extends Activity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View v = LayoutInflater.from(this).inflate(R.layout.your_layout, null);
        TextView textView = (TextView) v.findViewById(R.id.a_text_view);
        textView.setText("your text");
        addContentView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
    }
}

0
投票

有时是因为您期望LinearLayout为垂直方向,但是如果您未将其设置为垂直方向,则LinearLayout默认为水平方向,如果已经有一个具有匹配父级宽度的项目,那么您将看不到您的其他项目。


0
投票

这是因为父母的观点已经衡量了她的孩子。如果在测量后添加视图,则应致电

View.invalidate()

还要确保子视图的可见性为View.visible,并且view bounds不为0;

© www.soinside.com 2019 - 2024. All rights reserved.