我正在製作一個計算器UI。當屏幕進入橫向模式時,如何覆蓋和添加一些元素參數,但沒有製作單獨的佈局文件並複製所有元素?例如,我想改變一些按鈕的寬度和高度,也許添加一些額外的按鈕,但一切保持不變添加沒有單獨佈局文件的元素(Android)
我activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mat.calculator.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"></include>
</android.support.design.widget.CoordinatorLayout>
我content_main.xml在我的UI定義:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.mat.calculator.MainActivity"
tools:showIn="@layout/activity_main">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/infoTextViewTop"
style="@style/textViewSpace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:textSize="30sp"
android:text=" - - - "/>
</RelativeLayout>
<android.support.percent.PercentRelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button00"
android:text="mc"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button10"
android:text="m+"
android:layout_toRightOf="@id/button00"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button20"
android:text="m-"
android:layout_toRightOf="@id/button10"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button30"
android:text="mr"
android:layout_toRightOf="@id/button20"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button01"
android:text="C"
android:layout_below="@id/button00"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button11"
android:text="\u00F7"
android:layout_below="@id/button00"
android:layout_toRightOf="@id/button00"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button21"
android:text="\u00D7"
android:layout_below="@id/button00"
android:layout_toRightOf="@id/button10"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button31"
android:text="\u232B"
android:layout_below="@id/button00"
android:layout_toRightOf="@id/button20"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button02"
android:text="7"
android:layout_below="@id/button01"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button12"
android:text="8"
android:layout_below="@id/button01"
android:layout_toRightOf="@id/button00"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button22"
android:text="9"
android:layout_below="@id/button01"
android:layout_toRightOf="@id/button10"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button32"
android:text="\u2212"
android:layout_below="@id/button01"
android:layout_toRightOf="@id/button20"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button03"
android:text="4"
android:layout_below="@id/button02"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button13"
android:text="5"
android:layout_below="@id/button02"
android:layout_toRightOf="@id/button00"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button23"
android:text="6"
android:layout_below="@id/button02"
android:layout_toRightOf="@id/button10"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button33"
android:text="\u002B"
android:layout_below="@id/button02"
android:layout_toRightOf="@id/button20"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button04"
android:text="1"
android:layout_below="@id/button03"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button14"
android:text="2"
android:layout_below="@id/button03"
android:layout_toRightOf="@id/button00"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button24"
android:text="3"
android:layout_below="@id/button03"
android:layout_toRightOf="@id/button10"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button34"
android:text="\u003D"
android:layout_below="@id/button03"
android:layout_toRightOf="@id/button20"
app:layout_widthPercent="25%"
app:layout_heightPercent="32%" />
<Button
android:id="@+id/button05"
android:text="\u0025"
android:layout_below="@id/button04"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button15"
android:text="0"
android:layout_below="@id/button04"
android:layout_toRightOf="@id/button00"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
<Button
android:id="@+id/button25"
android:text="."
android:layout_below="@id/button04"
android:layout_toRightOf="@id/button10"
app:layout_widthPercent="25%"
app:layout_heightPercent="16%" />
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}