如何在其他布局上播放的LinearLayout中显示TextView?

时间:2011-01-01 10:26:21

标签: java android layout show

美好的一天。

我有三种布局:第一种是根,第二种和第三种是第一种。我尝试在第三个布局中添加TextView对象,并在第三个布局中添加了对象(我在debage模式下看到它)但是这个对象没有显示在屏幕上。

可能有人知道问题在哪里?

<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);
}

2 个答案:

答案 0 :(得分:0)

尝试像这样访问LayoutParams

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

并且你也声明linearLayout作为flulowing line上的局部变量使用它作为类变量。所以它是可以接触的方法。

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

答案 1 :(得分:0)

参考此示例示例

  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>

或使用以下链接

Creating Linear Layout with TextViews using a for loop