如何在首选项活动中添加自定义布局?

时间:2013-01-17 12:32:26

标签: android android-layout android-preferences

  

可能重复:
  How to add a button to PreferenceScreen

我想在首选项活动的底部添加自定义视图,还想实现在屏幕上添加的视图的onClick?当我尝试在首选项屏幕中使用布局充气器添加视图时,它始终位于首选项视图的顶部。但我想在偏好屏幕的底部显示我的观点。

以下是在偏好活动中添加布局的代码段。

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.bottom_preference, null, true);
RelativeLayout.LayoutParams rl=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
rl.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
addContentView(view,rl);

1 个答案:

答案 0 :(得分:1)

试试这个:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout parent = new RelativeLayout(this);

    Button button = new Button(this);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) generateLayoutParams(null);
    params.setMargins(35, 35, 0, 0);
    params.addRule(RelativeLayout.ALIGN_BOTTOM);
    button.setLayoutParams(params);

    parent.addView(button);

    setContentView(rl);
}