以编程方式创建的TextView未添加到RelativeLayout

时间:2016-10-07 07:31:11

标签: android android-layout textview relativelayout

我在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活动的输出顶部:

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}

2 个答案:

答案 0 :(得分:0)

使用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);

答案 1 :(得分:0)

首先,将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