如何设计此布局以适合所有方向?
有任何方法可以使用XML代码设计此布局(不使用背景图片)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/bar_all"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn_all"
android:layout_width="265dp"
android:layout_height="match_parent"
android:background="#00000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btn_ic_mypage"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:layout_marginRight="12dp"
android:background="#00000000"
android:scaleType="fitXY"
android:src="@drawable/icon_mypage" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
这是你要找的代码..
Also PL make sure you should avoid any length, height, width value is 20 dp,13 dp etc
此代码适用于所有屏幕尺寸。
<强> main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_main"
android:gravity="right|center_vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/linearLayout3"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center">
<Button
android:layout_height="match_parent"
android:id="@+id/button_shop"
android:layout_width="match_parent"
android:background="@null"></Button>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:background="@drawable/button"
android:id="@+id/button_doller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<强> MainActivity.java 强>
public class MainActivity extends Activity implements OnClickListener {
private Button btnShop = null;
private Button btnDoller = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShop = (Button) findViewById(R.id.button_shop);
btnShop.setOnClickListener(this);
btnDoller = (Button) findViewById(R.id.button_doller);
btnDoller.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (btnShop == v) {
Toast.makeText(this, "shop button clicked", Toast.LENGTH_LONG)
.show();
} else if (btnDoller == v) {
Toast.makeText(this, "doller button clicked", Toast.LENGTH_LONG)
.show();
}
}
}
PS:如果你得到的图像很小,那么创建一个更大的图像尺寸图标,我只使用了虚拟。
希望它有所帮助!!