【问题标题】:Android View Not AppearingAndroid 视图未出现
【发布时间】:2011-05-05 04:21:38
【问题描述】:

我正在尝试在我的主视图上显示一个新视图。

这是新视图的 XML:

    <RelativeLayout 
        android:id="@+id/mapdetaillayout" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"  
        android:gravity="left"
        xmlns:android="http://schemas.android.com/apk/res/android">

             <TextView 
                android:text="This is the Text from the XML File." 
                android:id="@+id/DetailTextView" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>

    </RelativeLayout>       

这是我用来将新 viev 推送到屏幕上的代码:

        RelativeLayout DetailLayout = new RelativeLayout(c);

        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.mapdetailview, DetailLayout);    

        TextView tv = (TextView) v.findViewById(R.id.DetailTextView);
        tv.setText("HERE IS THE TEXT FROM THE CODE");

        // This Log works
        Log.i("MESSAGE", tv.getText());

        DetailLayout.setBackgroundColor(Color.WHITE);

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.NO_GRAVITY;

        DetailLayout.bringToFront();
        DetailLayout.setVisibility(View.VISIBLE);

代码被调用,日志输出预期的文本,这对我来说表明视图已创建 - 它只是显示在屏幕上。

任何帮助将不胜感激。

【问题讨论】:

    标签: android visibility


    【解决方案1】:

    我在您的代码中没有看到对 Activity 的 setContentView() 方法的任何调用。也许你只是不这样做?如果是这样,这就是你什么都看不到的原因。此外,您不需要手动实例化 RelativeLayout。尝试将布局资源指定为内容视图:

    setContentView(R.layout.mapdetailview);
    

    然后您可以通过以下方式获取 TextView:

    TextView tv = (TextView) findViewById(R.id.DetailTextView);
    

    希望对你有帮助。

    【讨论】:

    • 你为什么要这样做?看起来对我来说完全不对。
    【解决方案2】:

    您是否尝试将 LayoutParams.WRAP_CONTENT 更改为 fill_parent 以查看是否有任何变化?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多