< 定義於.java >
在此想讓user按下畫面中的Button後, 能無限增加水平列的widget, 即使會超出螢寬
./res/layout/main.xml (這邊只擷取重點段)
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/content_area"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="40dp"
>
<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"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TableRow
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</TableLayout>
</HorizontalScrollView>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:text="button"
android:textSize="50sp"
/>
</LinearLayout>
./src/com.tsots.Hori_malloc/Hori_malloc.java
package com.tsots.Hori_malloc;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.TableRow;
import android.widget.TextView;
public class Activity_Hori_malloc extends Activity
{
Context context = Activity_Hori_malloc.this;
HorizontalScrollView scrollView;
TableRow row;
Button bt;
TextView tv1;
TextView tv2;
@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);
row = (TableRow) scrollView.findViewById(R.id.row);
bt = (Button) findViewById (R.id.bt);
//一樣預設是6個TextView, 不過宣告在.java
for (int i = 0; i < 6; i++)
{
tv1 = new TextView(context);
tv1.setText("text");
tv1.setHeight(80);
tv1.setWidth(280);
//文字的部份要置中
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(60);
//使可以被focus
tv1.setFocusable(true);
tv1.setFocusableInTouchMode(true);
//被focus時底色的變化
tv1.setBackgroundResource(R.drawable.selector_app_category);
//將widget加進TableRow中
row.addView(tv1);
}
//當user按下Button就新增一個widget在TableRow中
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
tv2 = new TextView(context);
tv2.setText("New Text");
tv2.setHeight(80);
tv2.setWidth(280);
tv2.setGravity(Gravity.CENTER);
tv2.setTextSize(60);
tv2.setFocusable(true);
tv2.setFocusableInTouchMode(true);
tv2.setBackgroundResource(R.drawable.selector_app_category);
//將圖加入TextView中:setCompoundDrawablesWithIntrinsicBounds(Drawable放左, Drawable放上, Drawable放右, Drawable放下) tv2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.bear3), null, null, null);
row.addView(tv2);
}
});
}
}
手指在HorizontalScrollView上做滑動或
adb shell input keyevent 22向右移動
可以看到新增加的widget
沒有留言:
張貼留言