PreferenceActivity 分屏显示 分析


先上效果图:



PreferenceActivity 设置代码如下,通过getListView 遍历视图结构,主要调整间距与背景色

package com.example.testsettings;

import java.util.List;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.LinearLayout;

public class MainActivity extends PreferenceActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		DEBUG("onCreate");
		System.out.println(isMultiPane());
		if (onIsMultiPane()) {// 可以分屏
			DEBUG("分屏显示");
		}

		ViewParent viewParent = getListView().getParent().getParent();
		LinearLayout linearLayout = (LinearLayout) viewParent;

		DEBUG(viewParent.toString());

		DEBUG(getListView().toString());
		// 与getListView() 的效果一样
		DEBUG(findViewById(android.R.id.list).toString());
		// ListView 背景
		getListView().setBackgroundColor(Color.BLUE);
		getListView().setPadding(10, 10, 10, 10);

		LinearLayout.LayoutParams params1 = (android.widget.LinearLayout.LayoutParams) getListView()
				.getLayoutParams();
		params1.setMargins(10, 10, 10, 10);

		DEBUG(findViewById(android.R.id.content).toString());
		// 根容器,包括ListView 和 右边
		View view = findViewById(android.R.id.content);
		view.setBackgroundColor(Color.GREEN);// 整个背景色

		// 容纳Listview的linearLayout
		ViewParent parent = getListView().getParent();
		LinearLayout linearLayout2 = (LinearLayout) parent;

		DEBUG(linearLayout2.getChildCount());
		DEBUG(linearLayout2.getChildAt(0).toString());
		DEBUG(linearLayout2.getChildAt(1).toString());

		// LinearLayout 放置 Listview的LinearLayout 和 右边的LinearLayout
		// 水平放置
		ViewParent parent2 = getListView().getParent().getParent();
		LinearLayout linearLayout3 = (LinearLayout) parent2;
		// 设置 水平分割线
		linearLayout3.setDividerDrawable(getResources().getDrawable(
				R.drawable.splite));
		linearLayout3.setDividerPadding(5);
		linearLayout3.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
		linearLayout3.setPadding(10, 10, 10, 10);

		DEBUG(linearLayout3.getChildCount());
		// 获取右边 PreferenceFrameLayout 容器的 上级
		View view2 = linearLayout3.getChildAt(1);
		view2.setBackgroundColor(Color.BLACK);// 右边容器的背景色
		view2.setPadding(0, 0, -30, 0);// 调整 内间距
		
		DEBUG(((LinearLayout) view2).getChildCount());
		LinearLayout linearLayout4 = (LinearLayout) view2;
		DEBUG(linearLayout4.getChildAt(1).toString());
		DEBUG("右边容器");
		// 获取 右边PreferenceFrameLayout 容器
		View view3 = linearLayout4.getChildAt(1);
		
		LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) view3
				.getLayoutParams();
		params.setMargins(-20, 10, 5, 5);// 调整外间距

		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onBuildHeaders(List<Header> target) {
		// TODO Auto-generated method stu
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值