我有这个问题。 我有一个活动:
public class MyActivity extends Activity
{
public View m_top_overlay;
private MyFrameLayout m_preview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
m_preview = (MyFrameLayout) findViewById(R.id.my_preview);
m_top_overlay = (View) findViewById(R.id.top_overlay);
m_frame_preview.setParent(this);
}
...
和xml:
<com.myexample.MyFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<View
android:id="@+id/top_overlay"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_gravity="top"
android:background="#FFFFFF"
/>
<com.myexample.MyFrameLayout />
然后,我的自定义视图继承自FrameLayout
public class MyFrameLayout extends FrameLayout
{
private MyActivity m_myActivity;
<constructors...>
public void setParent(MyActivity _activity)
{
m_myActivity = _activity;
}
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
ViewGroup.LayoutParams params = m_myActivity.m_top_overlay.getLayoutParams();
params.height = 200;
m_myActivity.m_top_overlay.setLayoutParams(params);
}
所以,我想要的是从java代码设置我的视图高度(m_top_view)。仅200的值就是例如。我会计算它。 但我描述的方式不起作用。