关于UI学习的总结
EditText的练习
MainActivity.java代码
package test.example.com.ch02_button;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } int size=30;//字体大小,初值为30 public void bigger(View v){ TextView txv; txv= (TextView) findViewById(R.id.txv);//强制类型转换 txv.setTextSize(++size); } public void smaller(View v){ if(size>30){ TextView txv= (TextView) findViewById(R.id.txv); txv.setTextSize(--size); } }}
电话簿UI练习
1.利用属性设置布局
2.按比例排布控件
3.改变控件字体颜色以及背景颜色
4.插入背景图片
activity_main.xml代码
MainActivity.java代码
package test.example.com.ch03_linearlayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends AppCompatActivity { EditText firstname,lastname,tel; TextView txv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); firstname=(EditText)findViewById(R.id.editText); lastname=(EditText)findViewById(R.id.editText1); tel=(EditText)findViewById(R.id.editText2); txv=(TextView)findViewById(R.id.textView3); } public void onClick(View v){ txv.setText(firstname.getText().toString()+lastname.getText().toString()+"的电话是"+tel.getText()); }}
变色程序
activity_main.xml代码
MainActivity.java代码
package test.example.com.chameleon_test;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import java.util.Random;public class MainActivity extends AppCompatActivity { TextView red,green,blue,hh; View Colorblock; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); red=(TextView)findViewById(R.id.txvR); green=(TextView)findViewById(R.id.txvG); blue=(TextView)findViewById(R.id.txvB); hh=(TextView)findViewById(R.id.hhh); Colorblock=findViewById(R.id.colorblock); } public void changecolor(View v){ Random x=new Random(); int r=x.nextInt(256); red.setText("红:"+r); red.setTextColor(Color.rgb(r,0,0)); int g=x.nextInt(256); green.setText("绿:"+g); green.setTextColor(Color.rgb(0,g,0)); int b=x.nextInt(256); blue.setText("蓝:"+b); blue.setTextColor(Color.rgb(0,0,b)); Colorblock.setBackgroundColor(Color.rgb(r,g,b)); hh.setTextColor(Color.rgb(r,g,b)); }}
下划线的解决问题
EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行。 EditText不支持marquee 用法如下: 在xml中 android:ellipsize = "end" 省略号在结尾 android:ellipsize = "start" 省略号在开头 android:ellipsize = "middle" 省略号在中间 android:ellipsize = "marquee" 跑马灯 android:singleline = "true"