gpt4 book ai didi

android - 在同一 LinearLayout 中更改另一个 TextView 时,TextView 会重新启动 Marquee

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:34 26 4
gpt4 key购买 nike

我有 LinearLayout 并且在 2 个 TextView 中都有选取框,当我在第一个中更新文本然后第二个 TextView 重新启动选取框时。

            <LinearLayout
android:id="@+id/panel"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:gravity="center_vertical"
android:orientation="vertical" >

<TextView
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="bottom|center_horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true" />

<TextView
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="top|center_horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
/>
</LinearLayout>

我发现如果为 R.id.firstR.id.second 我设置 layout_width="320dp" 效果不'发生。但是我想设置 android:layout_width="match_parent" 有一些解决方法吗?

我发现了类似的问题但没有解决方案: Android, RelativeLayout restarts Marquee-TextView when changing ImageView in same RelativeLayout

最佳答案

我遇到了类似的问题,解决方案是为 Textview 设置固定大小。

那么为什么不以编程方式进行呢?就我而言,它解决了问题。这是我的详细解决方案:

布局有点复杂,有很多变化的值。这是有趣的部分:

布局.xml :

<!-- The height and visibility values change programatically -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:visibility="gone" >

<FrameLayout>
...
// some code
...
</FrameLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent" />

<!-- My scrolling textview. see below. -->
<!-- The size will be set when -->
<!-- the layout will be draw, -->
<!-- after the Activity.onCreate(). -->
<!-- I removed ALL THE UNECESSARY (I mean -->
<!-- scrollHorizontally, focusable and focusableInTouchMode. -->
<!-- You don't need it !!!!) -->
<fr.cmoatoto.android.widget.ScrollingTextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever" />

<ImageView
...
// some code
... />
</LinearLayout>
</RelativeLayout>

ScrollingTextView 已经在这个 answer 中定义了

又来了:

滚动 TextView .java :

public class ScrollingTextView extends TextView {

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

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

public ScrollingTextView(Context context) {
super(context);
}

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}

@Override
public boolean isFocused() {
return true;
}
}

最后是 Activity 。正如我之前所说,您需要设置固定的宽度和高度,因此我们将通过 onCreate() 中的监听器以编程方式完成此操作:

MyActivity.java :

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

setContentView(R.layout.layout);


TextView textView = ((TextView) findViewById(R.id.home_trafic_text));
textView.setText(getString(R.string.loading));
textView.setEnabled(true); // Thanks to Romain Guy
textView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
LayoutParams params = v.getLayoutParams();
params.width = right - left;
params.height = bottom - top;
params.weight = 0;
v.removeOnLayoutChangeListener(this);
v.setLayoutParams(params);
}
});
}

如果您需要改变方向或类似的东西,请小心,但它对我来说效果很好!

----编辑 PRE-API-11---

因为 OnLayoutChangeListener 仅存在于 Api v11 中,所以有一个解决方法(它有效但我认为它不太好):

从您的 Activity 中删除 OnLayoutChangeListener:

MyActivity.java :

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

setContentView(R.layout.layout);

TextView textView = ((TextView) findViewById(R.id.home_trafic_text));
textView.setText(getString(R.string.loading));
textView.setEnabled(true); // Thanks to Romain Guy
}

并在您的 ScrollingTextView 中添加一个 onSizeChanged :

滚动 TextView .java :

public class ScrollingTextView extends TextView {

...
// Same code as before
...

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
LayoutParams params = (LayoutParams) getLayoutParams();
params.width = w;
params.height = h;
params.weight = 0;
setLayoutParams(params);
}
}

希望对你有帮助!

关于android - 在同一 LinearLayout 中更改另一个 TextView 时,TextView 会重新启动 Marquee,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11856875/

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