< 事先定義於.xml >
假設畫面上需要水平擺放8個TextView, 但寬度只能呈現6個
./res/layout/main.xml (這邊只擷取重點段)
<HorizontalScrollView
android:id="@+id/scrollView"android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_marginTop="40dp"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
>
<!-- HorizontalScrollView需要一個layout來輔助widget的排列方式, LinearLayout在此擔任orientation="horizontal"(水平排列)的角色 --> <LinearLayoutandroid:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- focusable與focusableInTouchMode必須為true才能focus此widget -->
<!-- 背景background鎖定./res/drawable/selector.xml其後解釋 -->
<TextView
android:id="@+id/tv1"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
<TextView
android:id="@+id/tv3"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
<TextView
android:id="@+id/tv4"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
<TextView
android:id="@+id/tv5"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
<TextView
android:id="@+id/tv6"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
<TextView
android:id="@+id/tv7"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
<TextView
android:id="@+id/tv8"
android:layout_width="280dp"
android:layout_height="80dp"
android:text="text"
android:textSize="60dp"
android:gravity="center_horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/selector"
/>
</LinearLayout>
</HorizontalScrollView>
</HorizontalScrollView>
./res/drawable/selector.xml 定義行為
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- focus成立, 但還未click -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/label_focus" />
<!-- focus成立, 也做了click -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/label_selected" />
</selector>
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/label_focus" />
<!-- focus成立, 也做了click -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/label_selected" />
</selector>
./src/com.tsots.Hori_basic/Hori_basic.java
package com.tsots.Hori_basic;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
public class Activity_Hori_basic extends Activity
{
Context context = Activity_Hori_basic.this;
HorizontalScrollView scrollView;
TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8;
@Override
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
tv5 = (TextView) findViewById(R.id.tv5);
tv6 = (TextView) findViewById(R.id.tv6);
tv7 = (TextView) findViewById(R.id.tv7);
tv8 = (TextView) findViewById(R.id.tv8);
}
}
沒有留言:
張貼留言