我正在尝试在单击按钮时动态添加图像按钮。当图像按钮的数量超过屏幕宽度时,我应该能够水平滚动。我试图实现Jess-Ander 的 TwoWayGridView但没有成功。我是初学者。因此,如果错误太初级,请多多包涵。
<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=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button" />
<com.jess.ui.TwoWayGridView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E8E8E8"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:columnWidth="80dp"
app:rowHeight="80dp"
app:numColumns="auto_fit"
app:numRows="2"
app:verticalSpacing="16dp"
app:horizontalSpacing="16dp"
app:stretchMode="spacingWidthUniform"
app:scrollDirectionPortrait="vertical"
app:scrollDirectionLandscape="horizontal"
app:gravity="center">
<LinearLayout
android:id="@+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.jess.ui.TwoWayGridView>
</LinearLayout>
这是代码:
package com.example.dynamic;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
LinearLayout linearLayout1;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
}
public void onClick(View v){
ImageView image = new ImageView(MainActivity.this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout1.addView(image);
}
}