如何在选项卡中为按钮使用自定义布局?

时间:2013-12-04 20:44:45

标签: android android-layout button android-custom-view

我制作了一个我想用作按钮的自定义布局(如标签)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:orientation="horizontal"
    android:layout_width="200dp"
    android:layout_height="48dp"
    android:gravity="center"
    android:background="@color/Blue_Background">

    <ImageView android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.6"
        android:padding="7dp"
        android:paddingLeft="20dp"
        android:src="@drawable/car_icon"
        android:gravity="right"
        />

    <TextView android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:text="Car Model"
        android:gravity="left"
        android:textColor="@color/White"
        android:textSize="20sp"
        />

</LinearLayout>

结果如下:Custom button layout

我想在水平滚动视图中使用此布局,作为选项卡,以编程方式添加新的关键按钮。我怎么能这样做?

Custom button layout Custom button layout Custom button layout

我不知道如果我解释得很好。谢谢你的时间。

3 个答案:

答案 0 :(得分:1)

// try this way and let me know still getting any stuff
1. you have already declare custom xml for your cutom button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:orientation="horizontal"
    android:layout_width="200dp"
    android:layout_height="48dp"
    android:gravity="center"
    android:background="@color/Blue_Background">

    <ImageView android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.6"
        android:padding="7dp"
        android:paddingLeft="20dp"
        android:src="@drawable/car_icon"
        android:gravity="right"/>

    <TextView android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:text="Car Model"
        android:gravity="left"
        android:textColor="@color/White"
        android:textSize="20sp"/>

</LinearLayout>
2. create custom properties for your custom button "attrs.xml"
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomButton">
        <attr name="caption" format="string" />
        <attr name="left_drawable" format="integer" />
    </declare-styleable>
</resources>
3. create custom class for your custom button
public class CustomButton extends LinearLayout {

    private LinearLayout base;
    private ImageView image;
    private TextView text;

    private String buttonCaption;
    private int buttonImage;

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context,attrs);
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public CustomButton(Context context) {
        super(context);
        init(context,null);
    }

    private void init(Context mContext,AttributeSet attributeSet) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.custom_button, null);


        base = (LinearLayout) v.findViewById(R.id.base);
        image = (ImageView) v.findViewById(R.id.image);
        text = (TextView) v.findViewById(R.id.text);

        if(attributeSet!=null){
            String namespace = "http://schemas.android.com/apk/res/" + getContext().getPackageName();
            buttonCaption = attributeSet.getAttributeValue(namespace, "caption");
            buttonImage = attributeSet.getAttributeIntValue(namespace, "left_drawable", R.drawable.ic_launcher);
        }

        addView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        setActionListener();
    }

    private void setActionListener() {

       base.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
    }

    public void setButtonCaption (String caption){
        buttonCaption = caption;
    }
    public void setButtonLeftDrawable(int drawable){
        buttonImage = drawable;
    }

    public String getButtonCaption (){
        return text.getText().toString();
    }
    public int getButtonLeftDrawable(){
        return buttonImage;
    }

    private void updateView(){
        image.setImageResource(buttonImage);
        text.setText(buttonCaption);
    }
}
4. use this custom button in xml
<?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/com.example.Demo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

   <com.example.Demo.CustomButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:caption="button"
       app:left_drawable="@drawable/ic_launcher"/>


</LinearLayout>
5. also change custom button properties at run time like normal button.
public class MyActivity extends Activity {

    private CustomButton customButton;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        customButton = (CustomButton) findViewById(R.id.mtCustomButton);

        customButton.setButtonCaption("myCustomButton");
        customButton.setButtonLeftDrawable(R.drawable.ic_launcher);
    }

}

答案 1 :(得分:0)

首先,您可以使用背景(紫色)和drawableLeft属性在单个Textview标记中使用您的布局。

然后,完成后,您应该查看Styles and Themes,因为那样,您需要的是将相同的样式应用于所有文本视图。

答案 2 :(得分:0)

我认为,当您说要将自定义布局用作“标签”时,您的意思是要创建一个带有可滚动标签的基于标签的视图导航。 如果是,那么请查看Android开发者网站上的以下培训主题,其中显示了如何创建基于标签的滑动视图:

http://developer.android.com/training/implementing-navigation/lateral.html

要自定义标签,您可以查看PagerTitleStripPagerTabStrip,因为它们是包含标签的视图组。