2016-10-07 135 views
0

我已經在我的activity_main.xml中添加了一個編程創建的RelativeLayout rl。當我嘗試在rl中添加一個TextView時,它不會被渲染。我已經記錄了這些值,rl正在呈現一個特定的寬度和高度,並且rl的globalLayoutListener被正確調用,但是不會呈現TextView。爲什麼?以編程方式創建的TextView不會被添加到RelativeLayout

這是我的MainActivity代碼:亞行外殼dumpsys活性最高的

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

輸出:

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: 

[email protected]452 

m[email protected]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} 
+0

[this](http://stackoverflow.com/a/10666269/5296734)可以幫助您 –

+0

@ nick7291它用於已存在的RelativeLayout。它不適用於動態創建的。 – Siva

+0

'adb shell dumpsys activity top'的輸出是什麼? – pskink

回答

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); 
+0

它仍然沒有呈現。 – Siva

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

相關問題