我已經爲我的應用程序創建了一組按鈕..現在我無法管理這些按鈕陣列的佈局..由於每當我添加圖像或chnage的寬度,它會從設備的水平屏幕熄滅..所以有任何管理這些按鈕陣列的方法,以便它們可以適應任何屏幕尺寸..我寫的代碼是..對於XML如何佈局按鈕陣列以適合任何屏幕尺寸
代碼:
public class CalenderForm extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createCalender();
}
public void createCalender()
{
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[6][7];
int count=43;
for (int i = 0; i<6; i++)
{
if(count%7==1)
{
rowLayout = new LinearLayout(this);
layoutVertical.addView(rowLayout,p);
count=count-7;
}
for(int j=0;j<7;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setBackgroundResource(R.drawable.icon);
rowLayout.addView(buttons[i][j], p);
}
}
}
:對Java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/liVLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:id="@+id/liVLayout1"
android:orientation="vertical"
android:layout_below="@+id/liVLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="All Contacts"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:background="#808080">
</TextView>
</LinearLayout>
</RelativeLayout>
代碼
}
快照befor插入圖片:
快照befor插入圖片:
1.嘗試減少的按鈕之間的間距。 2.嘗試使用相對佈局.. – Vinay
當我使用相對佈局沒有任何東西只有一個按鈕顯示.. – AndroidDev
您可以添加任何數量的按鈕,你想在相對佈局..可能你有其他問題 – Vinay