开发者

Adding view programatically to a FragmentActivity is not working

I have an Activity that extends FragmentActivity (it originally extended Activity but I changed this - the reason isn't really important, but I included this in case it helps track down the problem). Some of the information it displays varies depending on a particular variable, so I have a LinearLayout in the activity's layout xml to hold this information, and add it programatically from this class. This used to work fine, but has now stopped working and I can't see any reason why.

Here is an excerpt from the file:

public class FriendProfile extends FragmentActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendprofile);

        Intent intent = getIntent();
        userData = (ArrayList<String>)intent.getSerializableExtra("userData");
        String status = userData.get(4).trim();

        开发者_Go百科LinearLayout extras = (LinearLayout)findViewById(R.id.friendextra);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        if(status.equals("requester")) {
            Log.v(TAG, "requester");
            TextView view = new TextView(this);
            view.setText(R.string.requestpending);
            view.setLayoutParams(params);
            view.setGravity(Gravity.CENTER);
            extras.addView(view);
        }
    }
}

And an excerpt from the friendprofile.xml file:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/friendextra"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:gravity="center"  
        android:orientation="vertical" >
    </LinearLayout>

The logcat doesn't show anything interesting - it definitely enters the if block, because the log prints 'requester', but the textview doesn't show. There are several values that status can take, each of which will add different components to the layout, but none are working. The application doesn't crash or anything, it just doesn't display the components. Can anyone help?


I finally tracked down the source of the problem - in the friendprofile.xml file there is a TableLayout about the LinearLayout and its height was set to fill_parent. The LinearLayout was updating correctly, but was off screen. I changed the TableLayout's height to wrap_content, and that solved half the problem.

I still couldn't get the text to display, so I put an empty text view in the LinearLayout and just programmatically set the text, and this worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜