第九章 Android-UI组件(2)

时间:2025-01-31 22:04:50

一、图像视图(ImageView

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:layout_gravity="center"
android:id="@+id/Tablelayout01">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="text1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt2"
android:text="text1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt3"
android:text="text1"/></LinearLayout>
<ImageView
android:id="@+id/iv"
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/plane"/>
</LinearLayout>

逻辑

package com.example.administrator.app1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity {
private Button btn1,btn2,btn3;
private int alpha=255;
private int curr=0;
private int[] colors={R.drawable.bg_grey,R.drawable.bg_login,R.drawable.frame,R.drawable.logo};
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.bt1);
btn2=(Button)findViewById(R.id.bt2);
btn3=(Button)findViewById(R.id.bt3);
btn1.setOnClickListener(new MyListener());
btn2.setOnClickListener(new MyListener());
iv=(ImageView)findViewById(R.id.iv);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(colors[curr++%colors.length]);
}
}); }
class MyListener implements View.OnClickListener{
@Override
public void onClick(View v) {
if(v.equals(btn1)) {
Log.i("tag","11111");
alpha-=20;
if(alpha>=255){
alpha=255;
}
Log.i("tag","11111222222");
} else if(v==btn2) {
Log.i("tag","2222");
alpha-=20;
if(alpha<=0)
{
alpha=0;
}
Log.i("tag","22222333333");
}
Log.d("tag", alpha+"");
iv.setImageAlpha(alpha);
} }
}

二、逻辑

package com.example.administrator.app1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
private ListView lv;
private List<String> list;
private ArrayAdapter aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
//新建数据源
list=new ArrayList<String>();
for(int i=0;i<=20;i++)
{
list.add("listview子项"+i);
}
//新建适配器,将数据绑定到适配器
aa=new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,list);
//试图加载适配器
lv.setAdapter(aa); }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.app2.MainActivity">
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:dropDownHeight="100dp"
android:completionHint=""
android:background="@color/colorAccent"
android:dropDownWidth="match_parent"
android:id="@+id/actv"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="btn"
android:id="@+id/btn"/>
</LinearLayout>