【问题标题】:How to add custom layout in preference activity? [duplicate]如何在偏好活动中添加自定义布局? [复制]
【发布时间】:2018-06-26 20:50:02
【问题描述】:

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

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

下面是在偏好活动中添加布局的代码sn-ps。

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);

【问题讨论】:

    标签: android android-layout android-preferences


    【解决方案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);
    }
    

    【讨论】:

    • 我希望这个答案对你有帮助
    • 这对我来说失败了(参数为空,在我得到Your content must have a ListView whose id attribute is 'android.R.id.list' 之后)
    • 对不起,那是很久以前的事了,我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多