gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:47:43 25 4
gpt4 key购买 nike

我做了一个自定义布局,我想将其用作按钮(如选项卡)。

<?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

我想在水平 ScrollView 中使用此布局作为选项卡,以编程方式添加新的自定义按钮。我该怎么做?

Custom button layout Custom button layout Custom button layout

我不知道我是否解释得很好。感谢您的宝贵时间。

最佳答案

// 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);
}

}

关于android - 如何为选项卡中的按钮使用自定义布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com