編輯:關於 Android 程式設計
手機屏幕的空間永遠不夠用。垂直方向放不下就交给 ScrollView,水平方向放不下就交给 HorizontalScrollView,兩個組件的用法幾乎一模一樣,都是作為父容器包裹超出屏幕的內容。
為了順便演示嵌套,我把 HorizontalScrollView 放進了 ScrollView 裡,一個例子同時練兩個組件。
<ScrollView
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/myscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none">
<LinearLayout
android:id="@+id/mylinear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@drawable/recommend_61">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#99cccccc"
android:scrollbars="none">
<LinearLayout
android:id="@+id/id_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</ImageView>
</LinearLayout>
</ScrollView>
這裡有個細節值得記住:ScrollView 的直接子容器只能是一個,所以我們必須用 LinearLayout 包一層來承放多個控件;android:scrollbars="none" 則用來隱藏那條並不好看的滾動條。
橫向滾動的內容通常是一組結構相同的單項,因此單獨定義 activity_index_gallery_item.xml:
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:orientation="vertical">
<ImageView
android:id="@+id/id_index_gallery_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="@drawable/home01" />
<TextView
android:id="@+id/id_index_gallery_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="哈哈"
android:textColor="#ff0000"
android:textSize="12dp" />
</LinearLayout>
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String str[] = { "1", "2", "3", "4", "5", "6", "7", "8" };
private int[] images = {
R.drawable.home01, R.drawable.home02,
R.drawable.home03, R.drawable.home04
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initBtn(); // 動態添加按鈕,撐起垂直滾動
initHorizontalScrollView(); // 動態添加橫向單項
}
private void initBtn() {
LinearLayout linear = (LinearLayout) super.findViewById(R.id.mylinear);
// 按鈕寬度充滿父容器,高度包裹內容
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < str.length; i++) {
Button btn = new Button(this);
btn.setText(this.str[i]);
btn.setId(i); // 用 position 作為 id,方便回調時識別
btn.setLayoutParams(params);
linear.addView(btn);
btn.setOnClickListener(new OnClickListenerImpl());
}
}
private void initHorizontalScrollView() {
LinearLayout mGallery = (LinearLayout) findViewById(R.id.id_gallery);
for (int i = 0; i < images.length; i++) {
View view = View.inflate(this, R.layout.activity_index_gallery_item, null);
ImageView img = (ImageView) view.findViewById(R.id.id_index_gallery_item_image);
img.setImageResource(images[i]);
img.setId(i);
TextView txt = (TextView) view.findViewById(R.id.id_index_gallery_item_text);
txt.setText("我是圖片" + i);
mGallery.addView(view);
}
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"您選擇了按鈕" + (v.getId() + 1) + "!",
Toast.LENGTH_SHORT).show();
}
}
}
這裡把原來冗長的 switch...case 收斂成一行字符串拼接,效果相同但代碼短了很多——這也是前面重構文章裡提到的思路:不要在初始化之後再靠 R.id 反查。實際項目中,滾動列表更推薦直接用 RecyclerView 加 LayoutManager,它能同時覆蓋橫向與縱向滾動,並自帶 View 複用機制。
深入解析 Android 中 RecyclerView Adapter 的封裝思路
前言 關於 Adapter 的封裝,網路上已有許多成熟開源庫可以直接取用,省下不少功夫。最近閒來無事,便想自己動手封裝一個。 問題所在 一般封裝時,通常可以簡化到下面這個
Android 官方開發文件中文版:動畫與轉場框架完整導覽
本文整理自 Android 官方開發文件 Training 系列中的 Transitions 單元,說明如何運用轉場框架,讓檢視的出現與消失變得流暢而連貫。 為什麼需要轉
Android 下的 LED 控制(下)——Linux 驅動層解析
這一篇接著聊 Linux 側的實現。上一篇已經確認:JNI 通過打開 /dev/led 這個設備節點,再用 ioctl 來控制 LED 的亮滅與蜂鳴器的鳴響。那麼問題就來
Android TextView 跑馬燈橫向滾動效果實作教學
在 Android 開發中,當 TextView 顯示的文字超出可用寬度時,我們往往希望文字能像跑馬燈一樣橫向滾動,把完整內容呈現給使用者。以下分享一個實際測試可行的做法