2017-04-05 41 views
0

我正在製作一個計算器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); 
    } 
} 

回答

0

改變高度/寬度我很容易,打開(或創建)values.xmlvalues文件夾下,一個維度添加到它,像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <dimen name="btn_width">20%</dimen> 
</resources> 

新創建一個名爲values-land文件夾中的另一個values.xml(如果需要創建文件夾),並把下面的在它:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <dimen name="btn_width">30%</dimen> 
</resources> 

現在,在您的佈局xml文件,使用它像這樣:

android:layout_widthPercent="@dimen/btn_width" 

現在,如果你想ACTUA lly 更改佈局,如添加更多按鈕,您可以執行的操作是導出共享佈局塊(將按鈕矩陣定義爲新的xml文件「buttons.xml」,並將其包含在content_main.xml文件中。 在layout-land文件夾中創建一個新的佈局xml「包裝器」文件,其中包含所需的更改,並且還包括buttons.xml

您將添加一個新的佈局文件到layout-land,但他們將共享相同的內部佈局按鈕文件,因此您不會複製這兩個文件之間的任何按鈕。