Lun*_*lpo 5 android marquee android-linearlayout
我有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>
Run Code Online (Sandbox Code Playgroud)
我发现如果为R.id.first和R.id.second我设置layout_width="320dp"的效果不会发生.但我想设置android:layout_width="match_parent"一些解决方法?
我发现类似的问题,但没有解决方案: Android,RelativeLayout在同一RelativeLayout中更改ImageView时重新启动Marquee-TextView
小智 11
我有一个类似的问题,解决方案是设置Textview的固定大小.
那么为什么不按程序进行呢?就我而言,它解决了这个问题.以下是我的解决方案:
布局有点复杂,有很多变化的值.这是有趣的部分:
layout.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>
Run Code Online (Sandbox Code Playgroud)
ScrollingTextView已在此答案中定义
这又是:
ScrollingTextView.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;
}
}
Run Code Online (Sandbox Code Playgroud)
最后是活动.正如我之前所说,你需要设置固定的宽度和高度,以便我们将在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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果您需要更改方向或类似的东西,请小心,但它对我来说非常好用!
----编辑PRE-API-11 ---
因为OnLayoutChangeListener仅存在于Api v11,所以有一种解决方法(它有效,但我认为它不太好):
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
}
Run Code Online (Sandbox Code Playgroud)
并onSizeChanged在ScrollingTextView中添加一个:
ScrollingTextView.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);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望它有所帮助!
您应该阻止将两个选取框放在同一个ViewGroup中.在您的情况下,您可以使用LinearLayout包装每个选取框TextView(如果使用RelativeLayout,这将不起作用).
| 归档时间: |
|
| 查看次数: |
4768 次 |
| 最近记录: |