2012-11-23 56 views
0

我已经创建了扩展视图的类,我试图layout.addView(somthing)在布局中,没有例外被拖垮。addview到布局

布局

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" > 
</RelativeLayout> 

public class Navigator extends View { 
Context context; 
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.navigator); 

public Navigator(Context context) { 
    super(context); 
    this.context = context; 
} 

TextView tab1 = new TextView(context); 
tab1.setText("blah"); 
PROBLEM-> layout.addView(tab1); 
} 
+0

我尝试和惠特findViewById(R.id。****),但它不再工作。 –

回答

0

你将你的扩展视图的活动?

因为您正在修改您的视图,但我无法看到您将附加“导航器”视图的位置。

另一件事是你在XML上声明的RelativeLayout没有ID,所以你永远不会找到带有R.id.navigator id的RelativeLayout。

+0

:)其罚款白色id,我添加所需的行,但它不起作用。我在我的MainActivity中调用这个类。我调试和问题是在addView()它看起来像无法连接白色布局...如何添加视图在布局中的非活动类? –

+0

您需要设置新TextView的layout_width和height,否则它将在两个尺寸上都有0dp。 tx.setLayoutParams(new LayoutParams ....)); – noni

+0

不,我试过,没有工作,它的一切都很好,从我的MainActivity类添加textview,但在非活动类中,它不能连接到布局。 –