Я пытаюсь использовать 9-патч для рисования в качестве фона LinearLayout.
Когда я добавляю виды в макет в XML, я вижу фон в графическом макете Eclipse очень хорошо, нокогда я запускаю приложение, фон невидим.
вот код макета:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/quickaction_arrow_up" />
<ImageView
android:id="@+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/quickaction_arrow_left" />
<LinearLayout
android:id="@+id/tracks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/arrow_up"
android:layout_toRightOf="@id/arrow_left"
android:layout_centerVertical="true"
android:padding="5dp"
android:background="@drawable/action_popup"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<include layout="@layout/action_item" android:id="@+id/item1"/>
<include layout="@layout/action_item" android:id="@+id/item2"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/tracks"
android:src="@drawable/quickaction_arrow_right" />
<ImageView
android:id="@+id/arrow_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tracks"
android:src="@drawable/quickaction_arrow_down" />
</RelativeLayout>
и вот код для action_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="65dp"
android:layout_height="100dp"
android:gravity="center"
android:clickable="true"
android:padding="5dp"
android:background="@drawable/quickaction_slider_btn">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:scaleType="fitXY"
android:layout_weight="4"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:ellipsize="end"
android:textSize="12sp"
android:textColor="#666"/>
</LinearLayout>
Редактировать:
фрагмент кода, где я добавляю к макету:
protected void createActionList() {
WindowManager windowManager = (WindowManager)
m_context.getSystemService(Context.WINDOW_SERVICE);
final int screenWidth = windowManager.getDefaultDisplay().getWidth();
final int numItems = mActionItemList.size();
View view;
LinearLayout parent = null;
boolean needNewRow = true;
for( int i = 0; i < numItems; i++ ) {
if( i % m_numOfColumns == 0 && m_numOfColumns > 0 ) {
needNewRow = true;
}
if( needNewRow ) {
parent = new LinearLayout( m_context );
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT );
parent.setOrientation(LinearLayout.HORIZONTAL);
params.weight = 1;
parent.setLayoutParams( params );
mTrack.addView( parent);
}
view = mAdapter.getView( i, null, parent );
parent.addView(view);
parent.measure( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
int padding = mTrack.getPaddingLeft() + mTrack.getPaddingRight();
int parentWidth = parent.getMeasuredWidth() + padding;
int childWidth = parentWidth / parent.getChildCount();
needNewRow = false;
if( parentWidth + childWidth > screenWidth ) {
m_numOfColumns = -1;
needNewRow = true;
}
}
}
и код mAdapter.getView
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ActionItem action = (ActionItem)getItem(position);
String title = action.getTitle();
Drawable icon = action.getIcon();
View container = (View) inflater.inflate(R.layout.action_item, parent, false);
ImageView img = (ImageView) container.findViewById(R.id.iv_icon);
TextView text = (TextView) container.findViewById(R.id.tv_title);
if (icon != null) {
img.setImageDrawable(icon);
} else {
img.setVisibility(View.GONE);
}
if (title != null) {
text.setText(title);
} else {
text.setVisibility(View.GONE);
}
final int pos = mChildPos++;
final int actionId = action.getActionId();
container.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(MyQuickAction.this, pos, actionId);
}
if (!getActionItem(pos).isSticky()) {
mDidAction = true;
//workaround for transparent background bug
//thx to Roman Wozniak <roman.wozniak@gmail.com>
v.post(new Runnable() {
@Override
public void run() {
dismiss();
}
});
}
}
});
container.setFocusable(true);
container.setClickable(true);
return container;
}
вот фон макета с 9-патчами для рисования: