【问题标题】:Programatically created TextView not being added to RelativeLayout以编程方式创建未添加到 RelativeLayout 的 TextView
【发布时间】:2016-10-07 07:31:10
【问题描述】:

我在我的activity_main.xml 中添加了一个以编程方式创建的RelativeLayout rl。当我尝试在 rl 中添加 TextView 时,它没有被渲染。我已经记录了这些值,正在以特定的宽度和高度呈现 rl,并且正在正确调用 rl 的 globalLayoutListener,但未呈现 TextView。为什么?

这是我的 MainActivity 代码:

public class MainActivity extends AppCompatActivity {

    String TAG = MainActivity.class.getSimpleName();

    RelativeLayout parent, rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        parent = ((RelativeLayout) findViewById(R.id.activity_main));

        parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rl = new RelativeLayout(getApplicationContext());
                (parent).addView(rl);
                rl.setBackgroundResource(R.drawable.border);
                rl.getLayoutParams().width = parent.getMeasuredWidth();
                rl.getLayoutParams().height = parent.getMeasuredHeight();
                Log.d(TAG, "onCreate: "+rl.getLayoutParams().width+" "+rl.getLayoutParams().height);
                rl.requestLayout();

                rl.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        Log.d(TAG, "onGlobalLayout: rl");
                        TextView tv = new TextView(getApplicationContext());
                        tv.setText(MainActivity.class.getSimpleName());
                        tv.setX(20);
                        tv.setY(20);
                        tv.setWidth(200);
                        tv.setHeight(200);
                        tv.setTextSize(30);
                        rl.addView(tv);
                        rl.invalidate();
                        rl.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                });

                parent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
     }
}

adb shell dumpsys activity top 的输出:

TASK com.siva.textproject id=2335

ACTIVITY com.siva.textproject/.MainActivity 33e4fe6 pid=1587

Local FragmentActivity ffb1c0 State:

mCreated=truemResumed=true mStopped=false mReallyStopped=false

mLoadersStarted=true

FragmentManager misc state:

mHost=android.support.v4.app.FragmentActivity$HostCallbacks@470e452

mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@470e452

mCurState=5 mStateSaved=false mDestroyed=false

View Hierarchy:

com.android.internal.policy.PhoneWindow$DecorView{7e5e61c V.E..... ... 0,0-1440,2560}

 android.widget.LinearLayout{30e6323 V.E..... ... 0,0-1440,2560}

   android.view.ViewStub{f671020 G.E..... ... 0,0-0,0 #1020469 android:id/action_mode_bar_stub}

   android.widget.FrameLayout{e8bcdd9 V.E..... ... 0,84-1440,2560}

     android.support.v7.widget.ActionBarOverlayLayout{914f39e V.E..... ... 0,0-1440,2476 #7f0b0043 app:id/decor_content_parent}

       android.support.v7.widget.ContentFrameLayout{a5a0e7f V.E..... ... 0,196-1440,2476 #1020002 android:id/content}

         android.widget.RelativeLayout{dfd0e4c V.E..... ... 0,0-1440,2280 #7f0b0054 app:id/activity_main}

           android.widget.RelativeLayout{9e26e95 V.E..... ... 56,56-1384,2224}

             android.widget.TextView{f58abaa V.ED.... ... 0,0-200,200}

       android.support.v7.widget.ActionBarContainer{c8b139b V.ED.... ... 0,0 1440,196 #7f0b0044 app:id/action_bar_container}

         android.support.v7.widget.Toolbar{c12c338 V.E..... ... 0,0-1440,196 #7f0b0045 app:id/action_bar}

           android.support.v7.widget.AppCompatTextView{c85f11 V.ED.... ... 56,51-416,144}

           android.support.v7.widget.ActionMenuView{a6dd876 V.E..... ... 1440,0-1440,196}

         android.support.v7.widget.ActionBarContextView{9fd4e77 G.E..... ... 0,0-0,0 #7f0b0046 app:id/action_context_bar}

 android.view.View{5ccdae4 V.ED.... ... 0,0-1440,84 #102002f android:id/statusBarBackground}

【问题讨论】:

  • this可以帮你
  • @nick7291 它用于已经存在的RelativeLayout。它不适用于动态创建的。
  • adb shell dumpsys activity top 的输出是什么?
  • "View Hierarchy" 平吗?没有缩进?
  • @pskink 在这里粘贴时丢失了。添加了它们。

标签: android android-layout textview android-relativelayout


【解决方案1】:

将其维度设置为使用setLayoutParams()

LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
tv.setTextSize(30);
rl.addView(tv);

【讨论】:

  • 它仍然没有被渲染。
【解决方案2】:

首先,将TextView 定义为全局变量。比实例化你的RealativeLayout。之后设置你的LayoutParams。现在您可以将您的字符串设置为您的TextView

RelativeLayout rl = (RelativeLayout) findViewById(R.id.feed_relative_layout);
thereIsNoPost_textView = new TextView(Feed.this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
    thereIsNoPost_textView.setText(getResources().getString(R.string.feed_no_posts_to_show));
thereIsNoPost_textView.setTextSize((float) 20);
thereIsNoPost_textView.setLayoutParams(params);
thereIsNoPost_textView.setGravity(Gravity.CENTER);

rl.addView(thereIsNoPost_textView);

不要忘记将您的TextView 创建为全局变量。因为您要将其设置为 null 并清除代码其他部分中的 TextView

【讨论】:

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