【问题标题】:TextVIew doesn't appear or doesn't get added to LinearLayoutTextVIew 未出现或未添加到 LinearLayout
【发布时间】:2011-01-15 17:18:35
【问题描述】:

这是主要活动的代码。我在 addJoke() 方法中添加了 TextView,但它没有出现在 LinearLayout 中。请解决。

package edu.calpoly.android.lab2;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdvancedJokeList extends Activity {
    private static int i=0;
    //protected String m_strAuthorName;
    protected ArrayList<Joke> m_arrJokeList=new ArrayList<Joke>();
    //protected JokeListAdapter m_jokeAdapter;

    /**
     * ViewGroup used for maintaining a list of Views that each display Jokes.
     **/
    protected LinearLayout m_vwJokeLayout;
    protected EditText m_vwJokeEditText;
    protected Button m_vwJokeButton;
    protected int m_nDarkColor;
    protected int m_nLightColor;
    /**
     * Filter Options Submenu constants
     */
    protected static final int FILTER_OPTIONS = 1;
    protected static final int LIKE = Menu.FIRST + 1;
    protected static final int DISLIKE = Menu.FIRST + 2;
    protected static final int UNRATED = Menu.FIRST + 3;
    protected static final int SHOW_ALL = Menu.FIRST + 4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initLayout();
        String[] jokestring=getResources().getStringArray(R.array.jokeList);    
        for(String str : jokestring) 
        {
                Joke j=new Joke();
                j.setJoke(str);
                addJoke(j);
        }
        initAddJokeListeners();
    }

    protected void addJokeImplementation(){
        String strJoke=m_vwJokeEditText.getText().toString().trim();
        if(!strJoke.equals(""))
        {
        Joke joke=new Joke();
        joke.setJoke(strJoke);
        addJoke(joke);
        }
    }

    protected void initLayout() {
        setContentView(R.layout.advanced);
        m_vwJokeLayout=(LinearLayout)findViewById(R.id.jokeListViewGroup);
        m_vwJokeEditText=(EditText)findViewById(R.id.newJokeEditText);
        m_vwJokeButton=(Button)findViewById(R.id.addJokeButton);
    }

    protected void initAddJokeListeners() {
        // TODO
        m_vwJokeEditText.setOnKeyListener(new OnKeyListener(){
            @Override
            public boolean onKey(View v,int keyCOde, KeyEvent event){
                if(event.getAction()==KeyEvent.ACTION_DOWN)
                {
                    if(keyCOde==KeyEvent.KEYCODE_DPAD_CENTER)
                    {
                        addJokeImplementation();
                    }
                }
                return false;
            }

        });
        m_vwJokeButton.setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View view) {
        //Implement code to add a new joke here...
                addJokeImplementation();
            }
        });
    }

    protected void addJoke(Joke joke) {
         if(!m_arrJokeList.contains(joke))
          {
            m_arrJokeList.add(joke);
            //I also added textview here this one also
            //doesn't appear in Emulator layout, wondering whats wrong?;
            TextView TV=new TextView(this);
            TV.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            TV.setText(joke.toString());
            m_vwJokeLayout.addView(TV);
            m_nDarkColor=getResources().getColor(R.color.dark);
            m_nLightColor=getResources().getColor(R.color.light);       
            if(i==0)
            {
                TV.setBackgroundColor(m_nLightColor);
                i=1;
            }
            else
            {
                TV.setBackgroundColor(m_nDarkColor);
                i=0;
            }
            m_vwJokeEditText.setText("");
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
          }
    }         

    /**
     * Method used to retrieve Jokes from online server. The getJoke script
     * takes a single optional parameter, which should be encode in "UTF-8".
     * This parameter allows tells script to only retrieve Jokes whose author
     * name matches the value in the parameter.
     * 
     * param-1) "author": The author of the joke.
     * 
     * URL: http://simexusa.com/aac/getJokes.php?
     * 
     */
    protected void getJokesFromServer() {
        // TODO
    }

    /**
     * This method uploads a single Joke to the server. This method should test
     * the response from the server and display success or failure to the user
     * via a Toast Notification
     * 
     * The addJoke script on the server requires two parameters, both of which
     * should be encode in "UTF-8":
     * 
     * param-1) "joke": The text of the joke.
     * 
     * param-2) "author": The author of the joke.
     * 
     * URL: http://simexusa.com/aac/addJoke.php?
     * 
     * @param joke
     *            The Joke to be uploaded to the server.
     * 
     */
    protected void uploadJokeToServer(Joke joke) {
        // TODO
    }

}         

这里是advanced.xml;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/addJokeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Joke"
            />
        <EditText
            android:id="@+id/newJokeEditText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Joke"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/jokeListViewGroup"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

    </LinearLayout>
</LinearLayout>

更新:稍微修改了代码,添加了 Layout.params() 但 TextView 仍然没有出现。

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    两个原因;您需要直接或使用布局参数设置电视视图的高度和宽度,然后在完成此操作后,您需要再次调用 setContentView 但由于您添加的布局是根布局的子布局,您可能需要获取对根布局的引用,然后将 TV 视图添加到适当的子布局,然后设置内容视图(根布局)。

    【讨论】:

    • 如果视图树始终相同(即使有更多“叶子”),是否真的需要再次调用 setContentView()?
    • 最后一句没看懂....我已经将TextView添加到root Layout的child ....是这个问题吗?
    • 我已经参考了子布局并添加了TextView小部件,请参见代码,但仍然没有出现...动态添加TextView时是否需要设置高度和宽度参数?
    • 此错误在 04:06:05 E/hierarchyviewer 中蔓延:Adb 拒绝了设备 emulator-555 的转发命令 4:未找到设备 04:06:05 E/hierarchyviewer:Adb 拒绝了检查状态的命令设备 emulator-5554 上的视图服务器 04:06:05 E/hierarchyviewer:Adb 拒绝了在设备 emulator-5554 上启动视图服务器的命令 04:06:07 E/hierarchyviewer:Adb 拒绝了在设备 emulator-5554 上启动视图服务器的命令04:06:07 E/hierarchyviewer: Unable to debug device emulator-5554 emulator 5554 is android 1.6
    【解决方案2】:

    如果您想检查 如果您的视图是否存在,您会很高兴使用 Hierarchy-viewer。检查这个:http://developer.android.com/guide/developing/tools/hierarchy-viewer.html

    它会向您显示所有存在的视图,以及它们的放置方式。这应该对您进行这样的调试有很大帮助!

    【讨论】:

    • 我尝试使用 heirarchyviewer 但它给出了一个错误,即找不到 adb.exe...我认为这是因为新的 SDK R8... adb 位置更改为平台工具....任何解决方案..请帮助。
    • 是的,您应该将该目录添加到您的路径中。无耻的链接到我的博客关于解决方案:) androblip.huiges.nl/2011/01/10/hierarchy-viewer-problems
    • 另一个错误 04:06:05 E/hierarchyviewer:Adb 拒绝了设备模拟器 555 的转发命令 4:找不到设备 04:06:05 E/hierarchyviewer:Adb 拒绝了检查视图状态的命令设备 emulator-5554 上的服务器 04:06:05 E/hierarchyviewer:Adb 拒绝了在设备 emulator-5554 上启动查看服务器的命令 04:06:07 E/hierarchyviewer:Adb 拒绝了在设备 emulator-5554 上启动查看服务器的命令 04: 06:07 E/hierarchyviewer:无法调试设备 emulator-5554 emulator-5554 is android 1.6
    • 对于看到此 ABD 拒绝消息的任何人,最可能的原因是模拟器连接了其他东西。我使用 Debug 而不是 Run 从 Eclipse 启动应用程序。
    【解决方案3】:

    问题解决了……

    我很愚蠢....这太愚蠢了.... 我忘记为根元素添加 android:orientation="vertical" ..查看代码。

    我已经习惯了 Stackoverflow....我变得懒惰

    感谢所有优秀的开发人员,他们在我每次提出问题时都给出了一些很好的提示以及答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多