2017-12-18 106 views
0

我正在製作一個android版本的應用程序,基於iOS之一,但是,我在製作頂部導航欄時遇到了麻煩。我實際上想創建一個類似於iOS界面的操作欄,但是,我不能添加close buttongo back buttonnext button帶按鈕和標題的頂部導航欄

首先,如果是家庭活動,頂部導航欄將包含居中標題和用於關閉當前活動的close button。如下圖所示:

enter image description here

然後,如果用戶已經有了一個正確答案(或在某些條件下),然後next button會顯示出來,如果沒有,也不會顯示出來。

enter image description here enter image description here

就像上面的圖片,在圖片1,用戶可以回到Q1,爲Q1已經是正確的。此外,Q2也是正確的,因爲它可以進入Q3。然後,在圖2中,Q3尚未回答,但不能進入Q4,但可以進入Q2。而且,如果去Q2,圖片1的相同界面將顯示(仍然包含了下,和後退按鈕。

我現在有一個操作欄,但不能做任何事情,但只有一個標題。

的Manifest.xml:

<activity 
    android:name=".Problems" 
    android:configChanges= 
    "keyboard|keyboardHidden|orientation|screenLayout|uiMode| 
     screenSize|smallestScreenSize" 
    android:theme="@style/AccountSetting" 
    android:label="Problems" /> 

toolbarlayout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/chtl_toolbar" 
android:layout_width="match_parent" 
android:layout_height="?android:actionBarSize"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/chtl_useIMG" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_marginRight="20dp" 
     android:src="@mipmap/ic_launcher" 
     android:visibility="visible" /> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Nilu" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Nilu" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Nilu" /> 


</LinearLayout> 

</android.support.v7.widget.Toolbar> 

abs_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/app_name" 
     android:textColor="#000000" 
     android:id="@+id/mytext" 
     android:textSize="15sp" /> 
</LinearLayout> 

Main.java

public class Problems extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.chtl_toolbar); 
    setSupportActionBar(toolbar); 
    } 
} 

Style.xml

<style name="AccountSetting" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimaryDark">#ffffff</item> 
    <item name="colorPrimary">#ffffff</item> 
</style> 

回答

1

試試這個使用習慣Toolbar這樣

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/chtl_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?android:actionBarSize"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/chtl_useIMG" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_marginRight="20dp" 
      android:src="@mipmap/ic_launcher_round" 
      android:visibility="visible" /> 


     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Nilu" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Nilu" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Nilu" /> 


    </LinearLayout> 

</android.support.v7.widget.Toolbar> 

比你onCreate()設置此工具欄爲你的動作條活動方法

Toolbar toolbar = (Toolbar) findViewById(R.id.ar_toolbar); 
setSupportActionBar(toolbar); 

添加這個主題

<style name="AccountSetting" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimaryDark">@color/colorWhite</item> 
    <item name="colorPrimary">@color/colorWhite</item> 


</style> 

編輯你活動的問題佈局添加此

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <include 
     layout="@layout/toolbarlayout"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/app_name" 
     android:textColor="#000000" 
     android:id="@+id/mytext" 
     android:textSize="15sp" /> 
</LinearLayout> 

你的活動代碼

public class Problems extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_layout); 
    LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflator.inflate(R.layout.custom_actionbar, null); 
    getSupportActionBar().setCustomView(v); 
    } 
} 
+0

它不工作,能你給更多的細節LS? – androidnewbie

+0

@androidnewbie can you explain more –

+0

只需將'Toolbar'追加到我的'.xml'文件中,或者創建一個新的? – androidnewbie