Android 可点击布局

发布于 2024-10-09 08:50:00 字数 386 浏览 0 评论 0原文

我有一个线性布局,我已将其设置为 true 可点击 + 焦点,但问题是单击时没有显示焦点。我怎样才能让焦点显示出来。

这是我的代码

<LinearLayout
  android:id="@+id/linear_tv_layout"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:clickable="true"
  android:focusable="true"
  android:paddingBottom="7px">

I've got a linear layout that i have set true to be clikable + focus, But the problem is there is no focus displayed when clicked. How can i get the focus to be displayed.

Heres my code

<LinearLayout
  android:id="@+id/linear_tv_layout"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:clickable="true"
  android:focusable="true"
  android:paddingBottom="7px">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫瑾少女 2024-10-16 08:50:00

Material Design 的按钮按下动画应用于任何元素,将元素的 android 背景属性设置为:

android:background="?android:attr/selectableItemBackground"

在您的情况下:

<LinearLayout
  android:id="@+id/linear_tv_layout"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:clickable="true"
  android:focusable="true"
  android:paddingBottom="7px"
  android:background="?android:attr/selectableItemBackground">

To apply material design's button press animation to any element, set the element's android background attribute to:

android:background="?android:attr/selectableItemBackground"

In your case:

<LinearLayout
  android:id="@+id/linear_tv_layout"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:clickable="true"
  android:focusable="true"
  android:paddingBottom="7px"
  android:background="?android:attr/selectableItemBackground">
浊酒尽余欢 2024-10-16 08:50:00

我认为您需要将 状态列表drawable,它是一种drawable资源,你可以为不同的状态或状态组合指定不同的drawable,有一个用于选择,一个用于压缩等等。此外,布局的状态会传播到其所有子布局。


澄清 - 这是之前链接的文档中的示例:

res/drawable/button.xml:(它是可绘制的状态列表)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

button_pressedbutton_focusedbutton_normal 是代表这些状态下按钮的普通可绘制对象,可能是 png 的(因此按下的可以是插图,聚焦以橙色突出显示)。

如果您将此资源设置为“线性布局按钮”的背景:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button">
    ...
</LinearLayout>

现在聚焦布局将自动将其背景图像设置为@drawable/button_focused,依此类推。

当然,您使用的所有可绘制对象都必须已经是 res/drawable/ 中的资源,以及 button.xml

I think you need to set as background for the clickable view (the layout) a state list drawable , it's a drawable resource for which you can specify different drawables for different states or combinations of states, there's one for selection, one for pression and so on. Also, the state of a layout propagates to all its children.


CLARIFICATIONS - this is the example from the previously linked docs:

res/drawable/button.xml : (it's the state list drawable)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

button_pressed, button_focused and button_normal are normal drawables representing the button in those states, probably png's (so pressed could be inset, focused highlighted in orange).

if you set this resource as background to your "linear layout button":

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button">
    ...
</LinearLayout>

now focusing the layout will automatically set its background image to @drawable/button_focused, and so on.

of course, all the drawables you use must already be resources in res/drawable/, together with button.xml.

这个俗人 2024-10-16 08:50:00

@丹莱昂:
而不是
android:background="@android:drawable/btn_default"

你应该使用
android:background="@android:drawable/list_selector_background"

前者也会修改 UI,但这不是必需的。

@danLeon:
Instead of
android:background="@android:drawable/btn_default"

you should use
android:background="@android:drawable/list_selector_background"

former will modify the UI as well, which is not required.

缱倦旧时光 2024-10-16 08:50:00

尝试在代码中将该布局置于前面:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    View mainView = this.findViewById(R.id.mainView);
    this.setContentView(mainView);

    LinearLayout linear_tv_layout = (LinearLayout)mainView.findViewById(R.id.linear_tv_layout);
    linear_tv_layout.bringToFront();
    // or: mainView.bringChildToFront(linear_tv_layout);
}

如果这不起作用,请检查是否有一个或多个视图与该 LinearLayout 重叠。

Try to bring that layout to front in code:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    View mainView = this.findViewById(R.id.mainView);
    this.setContentView(mainView);

    LinearLayout linear_tv_layout = (LinearLayout)mainView.findViewById(R.id.linear_tv_layout);
    linear_tv_layout.bringToFront();
    // or: mainView.bringChildToFront(linear_tv_layout);
}

If that does not work, check if there is one or more view overlapped over that LinearLayout.

阳光①夏 2024-10-16 08:50:00

这是一个示例布局,可能可以给您一些想法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
  android:background="@android:drawable/btn_default"
  android:clickable="true"
  android:focusable="true"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1"
  >

  <ImageView
    android:id="@+id/image"
    android:src="@android:drawable/star_big_on"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>

<LinearLayout
  android:background="@android:drawable/btn_default"
  android:clickable="true"
  android:focusable="true"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1"
  >

  <ImageView
    android:id="@+id/image"
    android:src="@android:drawable/star_big_on"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>
</LinearLayout>

Heres a sample layout may be this can give you some idea:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
  android:background="@android:drawable/btn_default"
  android:clickable="true"
  android:focusable="true"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1"
  >

  <ImageView
    android:id="@+id/image"
    android:src="@android:drawable/star_big_on"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>

<LinearLayout
  android:background="@android:drawable/btn_default"
  android:clickable="true"
  android:focusable="true"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1"
  >

  <ImageView
    android:id="@+id/image"
    android:src="@android:drawable/star_big_on"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>
</LinearLayout>
伴随着你 2024-10-16 08:50:00

一个简单的方法是设置此属性:android:background="@android:drawable/btn_default"

<LinearLayout
  android:id="@+id/linear_tv_layout"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:clickable="true"
  android:focusable="true"
  android:paddingBottom="7px"
  android:background="@android:drawable/btn_default"
  >

A simple way is by setting this attribute: android:background="@android:drawable/btn_default"

<LinearLayout
  android:id="@+id/linear_tv_layout"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:clickable="true"
  android:focusable="true"
  android:paddingBottom="7px"
  android:background="@android:drawable/btn_default"
  >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文