标签:
<Button android:id="@+id/details_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="输入日期" android:textColor="@android:color/holo_red_light" android:gravity="center" android:background="@android:drawable/edit_text" />
private int mYear; private int mMonth; private int mDay; private OnDateSetListener mDateSetListener; private Button mDateButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // get the current date final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); mDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mDateButton.setText(getString(R.string.picked_date_format, monthOfYear, dayOfMonth, year)); } }; mDateButton=(Button)findViewById(R.id.details_date); mDateButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDatePickerDialog(); } }); } private void showDatePickerDialog(){ new DatePickerDialog(this,mDateSetListener,mYear, mMonth,mDay).show(); }总结:你也许会问,为什么不直接在EditText上设置点击事件,而要用一个Button去替代呢?因为使用Button更加安全,用户也不能修改Button的文字显示。你也可以使用TextWatcher来验证用户的输入,但是这将耗费更多的时间。在app中使用android的系统资源能非常好地利用设备的原有风格。
<TextView android:id="@+id/hello_world" android:textSize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/text_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_below="@+id/hello_world" android:layout_marginTop="4dp" android:text="我感觉我要喝点水,可你的嘴将我的嘴堵住" />这是java代码:
public class MainActivity extends AppCompatActivity { TextView helloWorldText; TextView textTwo; String textLink="visit <a href=\"http://www.sina.com/\">Sina Home</a>"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //第一个添加超链接 helloWorldText=(TextView)findViewById(R.id.hello_world); helloWorldText.setText(Html.fromHtml(textLink)); helloWorldText.setMovementMethod(LinkMovementMethod.getInstance()); //第二个改变前景色和背景色 textTwo=(TextView)findViewById(R.id.text_2); Spannable spannable=new SpannableString(textTwo.getText()); spannable.setSpan(new BackgroundColorSpan(Color.BLUE),2,5,0);//背景蓝色 int index=textTwo.getText().toString().indexOf(",");//获取“,”的位置 spannable.setSpan(new ForegroundColorSpan(Color.YELLOW),index,textTwo.getText().length(),0);//前景黄色 textTwo.setText(spannable); } }效果如下:点击Sina Home可以跳转到新浪首页
public class LedTextView extends TextView { private static final String FONTS_FOLDER = "fonts"; private static final String FONT_DIGITAL_7 = FONTS_FOLDER + File.separator + "digital-7.ttf"; public LedTextView(Context context) { super(context); init(context); } public LedTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public LedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { AssetManager assets = context.getAssets(); final Typeface font = Typeface.createFromAsset(assets,FONT_DIGITAL_7);//设置字体 setTypeface(font); } }然后在布局中设置两个LedTextView,一个用来显示88:88:88的背景,一个用来显示当前的时间,如下:
<merge xmlns:android="http://schemas.android.com/apk/res/android" > <com.manning.androidhacks.hack011.view.LedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="88:88:88" android:textColor="#3300ff00" android:textSize="80sp" /> <com.manning.androidhacks.hack011.view.LedTextView android:id="@+id/main_clock_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#00ff00" android:textSize="80sp" /> </merge>在activity中设置
public class MainActivity extends Activity { private static final String DATE_FORMAT = "%02d:%02d:%02d"; private static final int REFRESH_DELAY = 500; private final Handler mHandler = new Handler(); private final Runnable mTimeRefresher = new Runnable() { @Override public void run() { final Date d = new Date(); mTextView.setText(String.format(DATE_FORMAT, d.getHours(), d.getMinutes(), d.getSeconds())); mHandler.postDelayed(this, REFRESH_DELAY); } }; private TextView mTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView) findViewById(R.id.main_clock_time); } @Override protected void onResume() { super.onResume(); mHandler.post(mTimeRefresher); } @Override protected void onStop() { super.onStop(); mHandler.removeCallbacks(mTimeRefresher); } }其中%02d是用来限制数字格式的,2是代表宽度,如果整数不够2列就补上0,比如printf("%02d" ,3);结果就是03,
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#AAAAAA"/> <corners android:radius="15dp"/> </shape>
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView) findViewById(R.id.main_clock_time); Log.d("crx","before post:textView.width="+mTextView.getWidth()+",textView.height="+mTextView.getHeight()); mTextView.post(new Runnable() { @Override public void run() { Log.d("crx","after post:textView.width="+mTextView.getWidth()+",textView.height="+mTextView.getHeight()); } }); }下面输出的结果:
switch (getResources().getConfiguration().orientation) {//判断横竖屏 case Configuration.ORIENTATION_LANDSCAPE: {//如果是横屏 mPortraitContent.setVisibility(View.GONE);//隐藏竖屏控件 //设置VideoViews的高和宽等属性 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); params.addRule(RelativeLayout.CENTER_IN_PARENT); mVideoView.setLayoutParams(params); break; } case Configuration.ORIENTATION_SQUARE: case Configuration.ORIENTATION_UNDEFINED: case Configuration.ORIENTATION_PORTRAIT: default: {//竖屏 mPortraitContent.setVisibility(View.VISIBLE);//隐藏横屏控件 int[] locationArray = new int[2]; mPortraitPosition.getLocationOnScreen(locationArray);//获取控件高和宽 //设置视频空间高和宽 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mPortraitPosition.getWidth(), mPortraitPosition.getHeight()); params.leftMargin = locationArray[0]; params.topMargin = locationArray[1]; mVideoView.setLayoutParams(params); break; } }
<merge xmlns:android="http://schemas.android.com/apk/res/android" > <com.manning.androidhacks.hack011.view.LedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="88:88:88" android:textColor="#3300ff00" android:textSize="80sp" /> <com.manning.androidhacks.hack011.view.LedTextView android:id="@+id/main_clock_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#00ff00" android:textSize="80sp" /> </merge>在Hierarchy Viewer中查看此布局的层级关系图如下:
<resources> <style name="Theme.NoBackground" parent="android:Theme"> <item name="android:windowNoTitle">true</item> </style> </resources>把title去掉之后,布局层级关系如下图:
<resources> <style name="Theme.NoBackground" parent="android:Theme"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@null</item> </style> </resources>
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);让toast显示在屏幕的左上角
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout>然后在java代码中把布局添加到toast中并显示出来:
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("This is a custom toast"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
标签:
原文地址:http://blog.csdn.net/chenrenxiang/article/details/51137409