작업 표시 줄에 사용자 지정 검색을 표시하고 싶습니다 (저는이를 위해 ActionBarSherlock을 사용하고 있습니다).
나는 그것을 얻었다 :
그러나 사용 가능한 전체 너비를 차지하도록 사용자 정의 레이아웃 (edittext 필드)을 만들고 싶습니다.
여기에 제안 된대로 사용자 지정 레이아웃을 구현 했습니다 .
내 맞춤 레이아웃이 있습니다 search.xml
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:focusable="true" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|fill_horizontal" >
<EditText
android:id="@+id/search_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="@drawable/bg_search_edit_text"
android:imeOptions="actionSearch"
android:inputType="text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:src="@drawable/ic_search_arrow" />
</FrameLayout>
</LinearLayout>
그리고 MyActivity
:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_action_search);
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
actionBar.setCustomView(v);
사용 가능한 모든 너비를 차지하도록 사용자 지정 레이아웃을 만들려면 어떻게 actionBar
해야합니까?
도와주세요.
답변
이것에 대한 트릭이 있습니다. 당신이해야 할 일은 메인 컨테이너 RelativeLayout
대신 사용하는 것입니다 LinearLayout
. 그것을 android:layout_gravity="fill_horizontal"
설정하는 것이 중요 합니다. 그렇게해야합니다.
답변
나는 이것으로 어려움을 겪었고 Tomik의 대답을 시도했습니다. 그러나 이것은 레이아웃을 시작할 때 사용 가능한 전체 너비로 만들지 않았으며 뷰에 무언가를 추가 할 때만 가능했습니다.
LayoutParams.FILL_PARENT
보기를 추가 할 때 다음 을 설정해야합니다 .
//I'm using actionbarsherlock, but it's the same.
LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
getSupportActionBar().setCustomView(overlay, layout);
이렇게하면 사용 가능한 공간을 완전히 채 웁니다. (Tomik의 솔루션을 사용해야 할 수도 있습니다).
답변
이것이 나를 위해 일한 방식입니다 (위의 답변에서 기본 제목과 사용자 정의보기도 모두 표시되었습니다).
ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// actionBar.setCustomView(view); //last view item must set to android:layout_alignParentRight="true" if few views are there
actionBar.setCustomView(view, layout); // layout param width=fill/match parent
actionBar.setDisplayShowCustomEnabled(true);//must other wise its not showing custom view.
내가 눈치 것은 그 모두 setCustomView(view)
와 setCustomView(view,params)
보기 폭 = 경기 / 채우기 부모입니다. setDisplayShowCustomEnabled (부울 showCustom)
답변
Tomik과 Peterdk의 답변은 사용자 정의보기가 전체 작업 표시 줄 을 차지하고 기본 제목을 숨기고 싶을 때 작동합니다 .
그러나 사용자 정의보기가 제목과 나란히 표시되도록하려면 (그리고 제목이 표시된 후 남은 공간을 모두 채우려면) 여기에서 사용자 Android-Developer의 훌륭한 답변을 참조 할 수 있습니다.
https://stackoverflow.com/a/16517395/614880
하단의 그의 코드는 완벽하게 작동했습니다.
답변
예를 들어 EditText 요소를 포함하는 레이아웃 파일을 정의 할 수 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchfield"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textFilter" >
</EditText>
넌 할 수있어
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
search.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, "Search triggered",
Toast.LENGTH_LONG).show();
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
}
답변
Android의 런처 앱 (여기에서 라이브러리를 만들었습니다)에 월페이퍼 선택을 처리하는 클래스 ( “WallpaperPickerActivity”) 내에 예제가 있습니다.
이 예제는이 작업을 수행하려면 사용자 정의 된 테마를 설정해야 함을 보여줍니다. 슬프게도 이것은 지원 라이브러리가 아닌 일반 프레임 워크를 사용하여 저에게 효과적이었습니다.
테마는 다음과 같습니다.
styles.xml
<style name="Theme.WallpaperPicker" parent="Theme.WallpaperCropper">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowShowWallpaper">true</item>
</style>
<style name="Theme.WallpaperCropper" parent="@android:style/Theme.DeviceDefault">
<item name="android:actionBarStyle">@style/WallpaperCropperActionBar</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="WallpaperCropperActionBar" parent="@android:style/Widget.DeviceDefault.ActionBar">
<item name="android:displayOptions">showCustom</item>
<item name="android:background">#88000000</item>
</style>
값 -v19 / styles.xml
<style name="Theme.WallpaperCropper" parent="@android:style/Theme.DeviceDefault">
<item name="android:actionBarStyle">@style/WallpaperCropperActionBar</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="Theme" parent="@android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
편집 : 지원 라이브러리에서도 작동하는 더 나은 방법이 있습니다. 위에서 작성한 코드 대신 다음 코드 줄을 추가하면됩니다.
getSupportActionBar().setDisplayShowCustomEnabled(true);