2012-02-26 163 views
3

我有一个FrameLayout,随着时间的推移我想要增长。Android - 动态设置FrameLayout宽度

我已经实现了一个Runnable接口。

public void run() { 
    time_value++; 

    FrameLayout fl_dateTime = (FrameLayout)findViewById(R.id.game_DateTime); 
     LayoutParams lp_fl_dateTime = fl_dateTime.getLayoutParams(); 

    lp_fl_dateTime.width = time_value; 

    handler.postDelayed(this, 100); 
} 

为什么不能正常工作? O_O

回答

3

您需要在修改布局参数后重新设置布局参数。

View#setLayoutParams(ViewGroup.LayoutParams lp)

+0

此外,请确保您将您的runnable发布到UI线程,来自其他线程的UI操作未定义或非法或皱眉之类。看起来你正在使用处理程序,所以你可能是。 – nmr 2012-02-26 17:12:36

+0

嘿,谢谢你的答复,它工作得很好......买了一本关于Android开发的书,并试用了它......而且他们从未设置过布局参数......非常感谢:D – 2012-02-26 17:14:34

+0

http:// stackoverflow。 com/questions/1114287/good-book-for-beginning-android-development :) – nmr 2012-02-26 17:21:20

0

我能够通过设置帧0dp的初始大小,然后就设置为任何我想要的minWidth做到这一点。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:background="@android:color/black" 
      android:id="@id/layout" 
      android:minHeight="50dp" 
      android:measureAllChildren="false"> 

item.setMinimumWidth(10dp);