Android 开发 (控件)
<h1 id="android-开发-控件">Android 开发 (控件)</h1><h2 id="控件">控件</h2>
<h3 id="1textview">1.TextView</h3>
<blockquote>
<p>注意: 在其中 所有的配置应该写在values中 的 color strings 配置文件中</p>
</blockquote>
<table>
<thead>
<tr>
<th>TextView</th>
<th>属性</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>为textview设置一个组件id</td>
</tr>
<tr>
<td>text</td>
<td>文本</td>
</tr>
<tr>
<td>textstyle</td>
<td>样式</td>
</tr>
<tr>
<td>textsize</td>
<td>字体大小</td>
</tr>
<tr>
<td>textcolor</td>
<td>字体颜色</td>
</tr>
<tr>
<td>backgroup</td>
<td>背景颜色</td>
</tr>
<tr>
<td>width</td>
<td>组件宽度</td>
</tr>
<tr>
<td>heigh</td>
<td>组件高度</td>
</tr>
<tr>
<td>gravity</td>
<td>设置控件中内容的对齐方向</td>
</tr>
<tr>
<td>shadowcolor</td>
<td>设置阴影颜色 需要与shadowRadius 一起使用</td>
</tr>
<tr>
<td>shadowRadius</td>
<td>设置阴影的模糊程度 设置为0.1就变成了字体颜色了 建议使用3.0</td>
</tr>
<tr>
<td>shadowDx</td>
<td>设置阴影在水平方向的偏移 就是水平方向阴影开始的横坐标</td>
</tr>
<tr>
<td>shadowDy</td>
<td>设置阴影在竖直方向的偏移 就是竖直方向阴影开始的纵坐标</td>
</tr>
<tr>
<td>singLeLine</td>
<td>内容单行显示</td>
</tr>
<tr>
<td>foucusable</td>
<td>是否可以获取焦点</td>
</tr>
<tr>
<td>focusableInTouchMode</td>
<td>用于控制视图在触摸模式下是否可以聚焦</td>
</tr>
<tr>
<td>ellipsize</td>
<td>在哪里省略文本</td>
</tr>
<tr>
<td>marqueeRepeatLimit</td>
<td>字幕动画重复的次数</td>
</tr>
</tbody>
</table>
<h3 id="2button">2.Button</h3>
<table>
<thead>
<tr>
<th>Button</th>
<th>属性</th>
</tr>
</thead>
<tbody>
<tr>
<td>android:drawable</td>
<td>放一个drawable资源</td>
</tr>
<tr>
<td>android:drawableTop</td>
<td>可拉伸要绘制的文本的上面</td>
</tr>
<tr>
<td>android:drawableBottom</td>
<td>可拉伸要绘制的文本的下面</td>
</tr>
<tr>
<td>android:drawableLeft</td>
<td>可拉伸要绘制的文本的左侧</td>
</tr>
<tr>
<td>android:drawableRight</td>
<td>可拉伸要绘制的文本的右侧</td>
</tr>
<tr>
<td>android:text</td>
<td>设置显示的文本</td>
</tr>
<tr>
<td>android:textColor</td>
<td>设置显示文本的颜色</td>
</tr>
<tr>
<td>android:textSize</td>
<td>设置显示文本字体大小</td>
</tr>
<tr>
<td>android:background</td>
<td>可拉伸使用的背景</td>
</tr>
<tr>
<td>android:onClick</td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>button 状态</p>
<pre><code class="language-java">android:state_pressed//是否按下,如一个按钮触摸或者点击。
android:state_focused//是否取得焦点,比如用户选择了一个文本框。
android:state_hovered//光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected //被选中状态
android:state_checkable //组件是否能被check。如:RadioButton是可以被check的。
android:state_checked //被checked了,如:一个RadioButton可以被check了。
android:state_enabled //能够接受触摸或者点击事件
android:state_activated //被激活
android:state_window_focused //应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了
</code></pre>
<p>button 点击 长按 触摸事件</p>
<p>默认</p>
<pre><code class="language-java">public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button = findViewById(R.id.onebutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return false;
}
});
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
}
}
</code></pre>
<p>自定义</p>
<pre><code><Button
android:id="@+id/onebutton"
android:layout_width="200dp"
android:layout_height="200dp"
android:text="我是一个按钮"
android:onClick="oneclick"
/>
</code></pre>
<pre><code class="language-java"> //自定义单机事件 在MainActicity中
public void oneclick(View view) {
}
</code></pre>
<h3 id="3edittext">3.EditText</h3>
<table>
<thead>
<tr>
<th>EditText</th>
<th>属性</th>
</tr>
</thead>
<tbody>
<tr>
<td>hint</td>
<td>输入提示</td>
</tr>
<tr>
<td>textColorHint</td>
<td>输入提示文字的颜色</td>
</tr>
<tr>
<td>inputType</td>
<td>输入类型</td>
</tr>
<tr>
<td>drawableXxxx</td>
<td>在输入框的指定方位添加图片</td>
</tr>
<tr>
<td>drawablePaddding</td>
<td>设置图片与输入内容的间距</td>
</tr>
<tr>
<td>paddingXxxx</td>
<td>设置内容与边框的简洁</td>
</tr>
<tr>
<td>backgroup</td>
<td>背景色</td>
</tr>
</tbody>
</table>
<h3 id="4imageview">4.imageView</h3>
<table>
<thead>
<tr>
<th>imageView</th>
<th>属性</th>
</tr>
</thead>
<tbody>
<tr>
<td>src</td>
<td>设置图片资源</td>
</tr>
<tr>
<td>scaleType</td>
<td>设置图片缩放类型</td>
</tr>
<tr>
<td>maxHeight</td>
<td>设置最大高度</td>
</tr>
<tr>
<td>maxwidth</td>
<td>最大高度</td>
</tr>
<tr>
<td>adjustVireBounds</td>
<td>调整View的界限</td>
</tr>
</tbody>
</table>
<h3 id="5progressbar">5.ProgressBar</h3>
<table>
<thead>
<tr>
<th>ProgressBar</th>
<th>属性</th>
</tr>
</thead>
<tbody>
<tr>
<td>max</td>
<td>进度条最大值</td>
</tr>
<tr>
<td>progress</td>
<td>进度条已完成进度值</td>
</tr>
<tr>
<td>indeterminate</td>
<td>如果设置为true则进度条不精度显示</td>
</tr>
<tr>
<td>style="?android:attr/progressBarStyleHorizontal"</td>
<td>水平进度条</td>
</tr>
</tbody>
</table>
<h3 id="6notification-和-notificationmanage">6.Notification 和 NotificationManage</h3>
<table>
<thead>
<tr>
<th>Notification方法</th>
<th>作业</th>
</tr>
</thead>
<tbody>
<tr>
<td>setContentTitle</td>
<td>设置标题</td>
</tr>
<tr>
<td>setContentText</td>
<td>设置文本内容</td>
</tr>
<tr>
<td>setSmalllcon</td>
<td>设置小图标</td>
</tr>
<tr>
<td>setLargelcon</td>
<td>设置通知的大图标</td>
</tr>
<tr>
<td>setColor</td>
<td>设置小图标颜色</td>
</tr>
<tr>
<td>setContentIntent</td>
<td>设置点击通知后的跳转意图</td>
</tr>
<tr>
<td>setAutoCancel</td>
<td>设置点击通知后的自带清除通知</td>
</tr>
<tr>
<td>setWhen</td>
<td>设置通知创建的时间</td>
</tr>
</tbody>
</table>
<pre><code class="language-java">public class MainActivity extends AppCompatActivity {
private NotificationManager manage;
private Notification build;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建通知管理器
manage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel("leo", "测试通知", NotificationManager.IMPORTANCE_HIGH);
manage.createNotificationChannel(notificationChannel);
}
// 创建 通知
build = new NotificationCompat.Builder(this,"leo")
.setContentTitle("官方通知")
.setContentText("不要加载努力 结局不会演习")
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true)//设置自动删除通知
//.setLargeIcon() 设置右边的图片
//.setColor() 设置小图标的颜色
// .setContentIntent() 设置点击后跳转的地址
.build();
}
//发送通知
public void oneclick(View view) {
manage.notify(1,build);
}
//取消通知
public void twoclick(View view) {
manage.cancel(1);
}
}
</code></pre>
<h3 id="7toolbar">7.Toolbar</h3>
<table>
<thead>
<tr>
<th>backgroud</th>
<th>背景色</th>
</tr>
</thead>
<tbody>
<tr>
<td>navigationIcon</td>
<td>导航拦设置图标</td>
</tr>
<tr>
<td>title</td>
<td>主标题</td>
</tr>
<tr>
<td>titleTextColor</td>
<td>设置标题颜色</td>
</tr>
<tr>
<td>titleMarginStart</td>
<td>标题外边距</td>
</tr>
<tr>
<td>subtitle</td>
<td>子标题</td>
</tr>
<tr>
<td>subtitleTextColor</td>
<td>子标题颜色</td>
</tr>
<tr>
<td>logo</td>
<td>一个图标</td>
</tr>
</tbody>
</table>
<h3 id="8alterdialog">8.AlterDialog</h3>
<pre><code class="language-java"> public void threeclick(View view) {
//创建 对象
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setIcon(R.mipmap.ic_launcher)
.setTitle("标题")
.setMessage("Welcome to zzy")
.create()//创建
.show();//显示
}
</code></pre>
<table>
<thead>
<tr>
<th>setIcon</th>
<th>添加Icon</th>
</tr>
</thead>
<tbody>
<tr>
<td>setTitel</td>
<td>设置标题</td>
</tr>
<tr>
<td>setMessage</td>
<td>设置消息</td>
</tr>
<tr>
<td>setView</td>
<td>设置自定义布局</td>
</tr>
<tr>
<td>create</td>
<td>创建</td>
</tr>
<tr>
<td>show</td>
<td>显示</td>
</tr>
<tr>
<td>setPositiveButton</td>
<td>确定按钮</td>
</tr>
<tr>
<td>setNegativeButton</td>
<td>取消按钮</td>
</tr>
<tr>
<td>setNeutralButton</td>
<td>中间按钮</td>
</tr>
</tbody>
</table>
<h3 id="9-popupwinndow--悬浮框">9. PopupWinndow(悬浮框)</h3>
<table>
<thead>
<tr>
<th>setcContenView</th>
<th>设置popupWindow显示的view</th>
</tr>
</thead>
<tbody>
<tr>
<td>showASDropDown</td>
<td>相对某个控件的位置 无偏移</td>
</tr>
<tr>
<td>showAsDropDown</td>
<td>相对某个控件的位置有偏移</td>
</tr>
<tr>
<td>setFocusable</td>
<td>设置是否获得焦点</td>
</tr>
<tr>
<td>setBackgroundDranable</td>
<td>设置背景</td>
</tr>
<tr>
<td>dismiss</td>
<td>关闭弹窗</td>
</tr>
<tr>
<td>setAnimationStyle</td>
<td>设置加载动画</td>
</tr>
<tr>
<td>setTouchable</td>
<td>设置触摸功能</td>
</tr>
<tr>
<td>setOutsideTouchable</td>
<td>设置popupWindow 外面的触摸功能</td>
</tr>
</tbody>
</table>
<pre><code class="language-java">public void fourclick(View view) {
//获取显示页面
View inflate = getLayoutInflater().inflate(R.layout.popu_view, null);
//创建window弹窗对象
PopupWindow popupWindow = new PopupWindow(view, 200, 200, true);
//放入
popupWindow.setContentView(inflate);
//显示
popupWindow.showAsDropDown(view);
}
</code></pre><br><br>
来源:https://www.cnblogs.com/zhiy1202/p/15224776.html
頁:
[1]