我正在尝试创建scrum扑克应用程序,我有一个问题,在屏幕上平均传播图像按钮。我似乎并不完全了解特定的android布局是如何工作的。事实上,当我从xml文件“绘制”按钮时,我曾经使它工作,但现在我需要使用.png文件作为图像按钮。这是我的布局.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
style="@style/AppBaseTheme"
android:screenOrientation="portrait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity=""
android:animateLayoutChanges="true"
android:background="#f000"
android:baselineAligned="false"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/LinearLayout01"
style="@style/lineralLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
style="@style/lineralLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout3"
style="@style/lineralLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
以下是主要活动中我的代码的相关部分:
public final LinearLayout.LayoutParams rel_btn = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
FragmentTransaction ft = getFragmentManager().beginTransaction();
rel_btn.setMargins(0, 5, 0, 5);
switch (position) {
case 0:
fragment = new StandardFragment();
ft.addToBackStack(null);
ft.commit();
linearLayout01 = (LinearLayout) findViewById(R.id.LinearLayout01);
linearLayout2 = (LinearLayout) findViewById(R.id.LinearLayout2);
linearLayout3 = (LinearLayout) findViewById(R.id.LinearLayout3);
linearLayout01.removeAllViews();
linearLayout2.removeAllViews();
linearLayout3.removeAllViews();
layouts = new LinearLayout[3];
layouts[0] = linearLayout01;
layouts[1] = linearLayout2;
layouts[2] = linearLayout3;
ImageButton[] imageButton = new ImageButton[14];
for (int i = 0; i < imageButton.length; i++){
imageButton[i] = new ImageButton(this);
imageButton[i].setVisibility(View.VISIBLE);
imageButton[i].setLayoutParams(rel_btn);
imageButton[i].setId(i+100);
imageButton[i].setOnClickListener(this);
imageButton[i].setBackgroundColor(Color.TRANSPARENT);
imageButton[i].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String toParse = "0";
Intent intent = new Intent(MainActivity.this, DuzaKartaActivity.class);
intent.putExtra(TEXT_KEY, toParse);
startActivity(intent);
}
});
layouts[i % 3].addView(imageButton[i]);
}
imageButton[0].setImageResource(R.drawable.f0);
imageButton[1].setImageResource(R.drawable.f1);
imageButton[2].setImageResource(R.drawable.f1_2);
imageButton[3].setImageResource(R.drawable.f2);
imageButton[4].setImageResource(R.drawable.f3);
imageButton[5].setImageResource(R.drawable.f5);
imageButton[6].setImageResource(R.drawable.f8);
imageButton[7].setImageResource(R.drawable.f13);
imageButton[8].setImageResource(R.drawable.f20);
imageButton[9].setImageResource(R.drawable.f40);
imageButton[10].setImageResource(R.drawable.f100);
imageButton[11].setImageResource(R.drawable.infinity);
imageButton[12].setImageResource(R.drawable.coffee);
imageButton[13].setImageResource(R.drawable.dont_know);
Button buttonInvisible = new Button(this, null);
buttonInvisible.setTextAppearance(getApplicationContext(),
R.style.buttonText);
buttonInvisible.setLayoutParams(rel_btn);
buttonInvisible
.setBackgroundResource(R.drawable.btn_background_green);
buttonInvisible.setVisibility(View.INVISIBLE);
linearLayout3.addView(buttonInvisible);
break;
所以我想在每列中同样传播3行按钮5。最后一个是正确的显示purpouse隐形。我想根据设备扩展它们。目前我让它们显示得非常相似,但是当我使用边距时,这些按钮的图像被切割,如果我不使用边距,它们会重叠。
谢谢, 莱尼
编辑:这是当前输出的图片: