2015-05-29 44 views
6

Tôi đang cố gắng tạo thanh công cụ mở rộng tùy chỉnh trong android bằng văn bản chỉnh sửa trong thanh công cụ. Cách bố trí mà tôi muốn thực hiện trông giống như sauTạo thanh công cụ tùy chỉnh trong android

enter image description here

Mã mà tôi đã viết để thực hiện là một cái gì đó như thế này:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_height="256dp" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 

    > 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/searchbox" 
     android:layout_alignParentBottom="true" 
     android:text="Test" 
     android:background="#ffffff" 
     /> 

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

Và Hoạt động có sau mã

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
    if (toolbar != null) { 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
     getSupportActionBar().setDisplayShowHomeEnabled(false); 
    }} 

Nhưng w chiếc mũ tôi nhận được thay thế là: enter image description here

Không có nhiều hướng dẫn về tùy chỉnh thanh công cụ mở rộng để thực sự đánh giá cao một số trợ giúp.

+0

Có lẽ điều này có thể giúp bạn: http://stackoverflow.com/questions/31231609/creating -a-button-in-android-toolbar/31477092 # 31477092 – Machado

Trả lời

2

Tôi nghĩ rằng bạn chỉ cần thêm trọng lực = "bottom" trên các thiết lập thanh công cụ như:

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_awesome_toolbar" 
    android:gravity="bottom" 
    android:layout_height="256dp" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary"> 

tôi đã có thêm một số lợi nhuận để dưới cùng của cách bố trí để có được chỉnh sửa để xuất hiện nhưng điều đó nên nhận được Văn bản ở cuối bản chỉnh sửa.

Hoặc bạn có thể đặt layout_gravity trên EditText.

 <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:layout_gravity="bottom" 
     android:id="@+id/searchbox" 
     android:text="Test" 
     android:background="#ffffff"/> 

Tôi ngạc nhiên alignParentBottom biên dịch. Tôi không tin rằng kế thừa Thanh công cụ từ RelativeLayout.

Edit - Dưới đây là cách bố trí hoàn chỉnh của tôi:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:context=".MainActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_height="264dp" 
    android:layout_width="match_parent" 
    android:layout_alignParentBottom="true" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary"> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:layout_gravity="bottom" 
     android:id="@+id/searchbox" 
     android:text="Test" 
     android:background="#ffffff"/> 
</android.support.v7.widget.Toolbar> 
</RelativeLayout> 

nào dẫn đến điều này:

ToolBar

+0

Tôi cũng đã thử với điều đó. Nó hiển thị nó một cách kỳ lạ. Bạn có thể xem kết quả trong ảnh chụp màn hình này- https://dl.pushbulletusercontent.com/LKPBxwLnZez8neMwpuaNZXMmjTWbiDyO/Screenshot_2015-05-29-22-29-05.png Hơn nữa, vấn đề cũng là chiều rộng không được căn chỉnh đúng cách cả hai mặt của văn bản chỉnh sửa. –

+0

Sự cố này đã được nhiều người hơn xem - http://stackoverflow.com/questions/27007985/support-toolbars-custom-view-not-using-full-width http://stackoverflow.com/questions/29239254/extended -toolbar-with-custom-view-not-display-với-full-width Không thấy một giải pháp rõ ràng cho nó mặc dù. –

+0

http://stackoverflow.com/a/29648396/1268021 Dường như để giải quyết nó. –

Các vấn đề liên quan