1

android新手试图弄清楚,任何帮助表示赞赏。我有一个当前可点击的文本视图。我只需要为其添加一个可点击的按钮背景。这是我的 xml 文件:布局下的 mainscreen.xml

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@android:drawable/divider_horizontal"
            android:orientation="vertical"
            android:showDividers="middle" >


             <TextView
                android:id="@+id/mainscreen_option"
                style="@style/TextView.MainscreenItem"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginBottom="1dp"
                android:clickable="true"
                android:onClick="onMainscreenClicked"
                android:text="@string/nav_option"
                    />  
</LinearLayout>

我定义按钮的选择器类代码位于可绘制文件夹下:bg_button.xml

<?xml version="1.0" encoding="utf-8"?>


<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/menu_btn_active"/>
<item android:state_pressed="true" android:drawable="@drawable/menu_btn_active" />
<item android:drawable="@drawable/menu_btn" />  
</selector>

textview 对应的 javacode 是:mainscreennav.java

private void highlightMenuItem(){
TextView highlightedTextView = null;
final String activeFragmentTitle = getArguments().getString(ACTIVE_MENU_ITEM);
final Resources resources = Application.getAppResources();

if (resources.getString(R.string.nav_option_mainscreen).equals(FragmentTitle)) {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_mainscreen);
        } highlightedTextView.setTextColor(getResources().getColor(R.color.dark_orange));

    }
}

任何人都可以指导我如何将此文本视图转换为一个按钮,以便两者都可以一起点击,并且我可以将按钮中的文本边距从左侧设置为某些 dp。提前致谢!贾斯汀

4

2 回答 2

1

编辑:

使用您的代码创建一个选择器 xml 并放入您的可绘制文件夹中。我将创建一个 btn_custom.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@drawable/btn_active" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/btn_active" />
<item
     android:drawable="@drawable/btn_default" />
</selector>

然后,在您的 TextView 上:

<TextView android:text="MyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:clickable="true"
    android:background="@drawable/btn_custom"
    android:padding="10dp"/>

在填充时,您可以调整按钮填充。

在后台,输入您的选择器的名称(在我的例子中,@drawable/btn_custom)

你的效果已经开始了。

比,只需注册一个 onClickListener

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView myTextButton = (TextView) findViewById(R.id.my_button_id);
    myTextButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "ButtonClick", 200).show();
        }
    });
}

Ps:您可以改用 onClick 方法:P

永远记住:Button 只是一个“样式化”的 TextView。

这是 Button 类的 Android 源代码:

public class Button extends TextView {
    public Button(Context context) {
        this(context, null);
    }

    public Button(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.buttonStyle);
    }

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

是的,就是这样。

于 2013-05-16T21:45:06.497 回答
0

据我了解你的问题,你想在按钮前面有可点击的文本视图吗?如果是这样创建RelativeLayout 而不是LinearLayout,请将您的textview 放在按钮前面(或根据您的需要以其他方式)。并为他们两个分配 onClick。

希望这对您有所帮助并享受您的工作。

于 2013-05-16T21:02:11.530 回答