简体   繁体   中英

How do I show TextView's in LinearLayout that layed on other Layout?

Good day.

I have three layouts: first is the root, second and third lie in first. I try add TextView object in third layout and objects had been added in third layout (I saw it in debage mode) but this objects didn't showed on screen.

May be someone know where is the problem?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <LinearLayout

        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <Button
            android:id="@+id/addJokeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
        />

        <EditText
            android:id="@+id/newJokeEditText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />

    </LinearLayout>    

    <LinearLayout

        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

    protected void initLayout() {
    setContentView(R.layout.advanced);

    LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
            R.layout.advanced, null);
    m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
    m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);
    m_vwJokeLayout = (LinearLayout) linearLayout.getChildAt(1);
}

    protected void addJoke(Joke joke) {
    m_arrJokeList.add(joke);

    LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    TextView textView = new TextView(this);
    setColor(textView);
    textView.setLayoutParams(lparams);
    textView.setText(joke.getJoke());

    m_vwJokeLayout.addView(textView);
}

Try to access LayoutParams like this

LinearLayout.LayoutParams lparams = new    LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);

and also you are declaring linearLayout as local variable on fallowing line use it as class variable. so that it is accessible out side method.

    LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
            R.layout.advanced, null); 

Refer this sample example

  package com.example.helloandroid;

  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  import android.widget.LinearLayout.LayoutParams;

  public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout ll= (LinearLayout) findViewById(R.id.LinearLayout01); 


      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

      LinearLayout childLayout= new LinearLayout(this);
      childLayout.setOrientation(LinearLayout.VERTICAL);  
      TextView text = new TextView(this);
      text.setText("High");
      childLayout.addView(text);
      ll.addView(childLayout, lp);

}

}`

<?xml version="1.0" encoding="utf-8"?>

 <LinearLayout 
 android:id="@+id/LinearLayout01" 
 android:layout_width="wrap_content"   
 android:layout_height="wrap_content" 
  android:orientation="vertical"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <TextView 
  android:id="@+id/textview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="@string/hello"/>
</LinearLayout>

or make use of following link

Creating Linear Layout with TextViews using a for loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM