小编use*_*239的帖子

android:layout_gravity无法按预期工作

我有一个简单的LinearLayout,一个TextView和一个ImageView.我希望TextView中的文本与右侧对齐,但结果显示Text与左侧对齐.我的布局xml有什么问题吗?谢谢.

<?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="wrap_content" android:background="#E8E3E4">
    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        >
        <TextView android:layout_width="260dp" android:layout_height="wrap_content"
            android:text="More Comments" android:textStyle="bold" android:background="#ff0000"
            android:textColor="#121222" android:layout_gravity="right" />
        <ImageView android:layout_width="50dp"
            android:layout_height="wrap_content" android:src="@drawable/arrow_right"
            android:layout_gravity="center" android:scaleType="centerInside" />
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

谢谢,迈克尔.但是你提到的解决方法也不起作用.

我尝试使用RelativeLayout,但它仍然给我结果如下:更多评论(图标)

我期待的更多评论(图标)

RelativeLayout xml在下面.它有什么问题吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/label" android:layout_width="260dp"
        android:layout_height="wrap_content" android:text="More Comments"
        android:textStyle="bold" android:background="#ff0000"
        android:textColor="#121222" android:layout_gravity="right" />
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content"
        android:src="@drawable/arrow_right" android:layout_gravity="center"
        android:scaleType="centerInside" android:layout_toRightOf="@id/label" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

根据Michael的评论解决方法,我在下面有XML.我期望输出为:

     RightLeft
Run Code Online (Sandbox Code Playgroud)

但实际输出是:

Right             Left
Run Code Online (Sandbox Code Playgroud)

所以解决方法对我不起作用.有什么想法吗?

   <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
           <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_gravity="right" …
Run Code Online (Sandbox Code Playgroud)

java android

11
推荐指数
2
解决办法
1万
查看次数

为什么按钮在LinearLayout中没有垂直居中?

我有一个LinearLayout,它只包含一个按钮.我希望此按钮垂直居中并与右侧对齐.我尝试了很多方法,但我无法使这个按钮垂直居中.它始终与顶部对齐.我也尝试在RelativeLayout中放一个按钮,按钮也不能垂直居中.

XML如下.这个布局有什么问题吗?谢谢.

<?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="wrap_content" 
    android:background="#E8E3E4">
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="More" 
        android:layout_gravity="right" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

将android:layout_gravity ="right"更改为android:layout_gravity ="right | center_vertical"并未在我的问题中解决问题.

android

10
推荐指数
1
解决办法
2万
查看次数

有没有一个好方法来获得即将到来的星期三的日期?

有没有一个好方法来获得即将到来的星期三的日期?也就是说,如果今天是星期二,我想在本周得到星期三的日期; 如果今天是星期三,我想得到下周三的日期; 如果今天是星期四,我想在接下来的一周得到星期三的日期.

谢谢.

java calendar date

10
推荐指数
2
解决办法
1万
查看次数

如何摆脱按钮和其他视图之间的额外差距?

当我创建一个按钮视图时,Android总是在这个按钮和它下面的其他视图之间创建一些额外的空间.

在下面的示例中,第二个按钮上方有一个按钮.你可以看到这两个按钮之间的差距.我怎样才能摆脱这种差距?谢谢.

<?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="wrap_content">

    <Button android:id="@+id/up"
        android:layout_width="fill_parent" android:layout_height="28dip"
        android:textSize="9sp" android:gravity="center" android:text="Hide sth"
        android:layout_marginBottom="0" android:paddingBottom="0" />
    <Button android:id="@+id/down"
        android:layout_width="fill_parent" android:layout_height="28dip"
        android:textSize="9sp" android:gravity="center" android:text="Show sth" />
   </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android

7
推荐指数
2
解决办法
1万
查看次数

如何在代码中检测手机是否处于睡眠模式?

有没有办法检测代码中的Android手机是否处于睡眠模式(屏幕为黑色)?我写了一个主屏幕小部件.我不希望在屏幕为黑色时更新小部件以节省电池消耗.

谢谢.

android

6
推荐指数
1
解决办法
7845
查看次数

如何防止在TabActivity的第一个选项卡上启动活动?

我有一个TabActivity,其中包含4个活动.我的代码将第二个选项卡设置为当前选项卡:

public class MyTabActivity extends TabActivity {
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec; // Resusable TabSpec for each tab
    Intent intent; // Reusable Intent for each tab

    TextView tabView;

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Activity1.class);

    spec = tabHost.newTabSpec("Tab 1");
    spec.setContent(intent);
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null);
    tabView.setText("Tab 1");
    spec.setIndicator(tabView);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Activity2.class);
    spec = tabHost.newTabSpec("Tab 2");
    spec.setContent(intent);
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null);
    tabView.setText("Tab 2");
    spec.setIndicator(tabView); …
Run Code Online (Sandbox Code Playgroud)

android

6
推荐指数
1
解决办法
1693
查看次数

如何在主屏幕小部件中检测方向变化?

我正在编写一个主屏幕小部件,并希望在设备方向从纵向更改为横向或其他方式时更新主屏幕小部件.我该怎么做?

目前,我尝试像下面的代码一样重新配置CONFIGURATION_CHANGED操作,但Android不允许我这样做,说"不允许IntentReceiver组件注册接收意图".有人能帮我吗?谢谢.

public class MyWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        this.registerReceiver(this, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
    }
Run Code Online (Sandbox Code Playgroud)

android

5
推荐指数
2
解决办法
5913
查看次数

IntentService的意外行为

我在我的代码中使用了IntentService而不是Service,因为IntentService在onHandleIntent(Intent intent)中为我创建了一个线程,所以我不必在我的服务代码中创建一个Thead.

我期望同一个IntentSerivce的两个意图将并行执行,因为在IntentService中为每个发明生成一个线程.但我的代码证明了两个意图以顺序方式执行.

这是我的IntentService代码:

public class UpdateService extends IntentService {

    public static final String TAG = "HelloTestIntentService";

    public UpdateService() {
        super("News UpdateService");
    }

    protected void onHandleIntent(Intent intent) {

        String userAction = intent
        .getStringExtra("userAction");

        Log.v(TAG, "" + new Date() + ", In onHandleIntent for userAction = " + userAction + ", thread id = " + Thread.currentThread().getId());

        if ("1".equals(userAction)) {
            try {
                Thread.sleep(20 * 1000);
            } catch (InterruptedException e) {
                Log.e(TAG, "error", e);
            }

            Log.v(TAG, "" + new Date() + ", …
Run Code Online (Sandbox Code Playgroud)

android

5
推荐指数
3
解决办法
6143
查看次数

创建多个线程池(ExecutorService)可以吗?

我在代码中创建了多个 ExecutorService 实例,通常每个 UI 页面都有一个 ExecutorService 实例。每个ExecutorService实例都会执行一些http get请求线程。

private ExecutorService m_threadPool = Executors.newCachedThreadPool();
Run Code Online (Sandbox Code Playgroud)

这样做可以吗?

我遇到的问题是,有时 http get 请求从 HttpURLConnection getResponseCode() 调用中获得响应代码 -1。不知道是否是多个线程池实例导致的。

谢谢。

java android

5
推荐指数
1
解决办法
4979
查看次数

是否可以将事件从线程发送到活动?

如果我想将一个事件(例如OnClick)发送到线程中的活动?谢谢.

预期的工作流程如下:

public class HelloAndroid extends Activity {

   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Crate threadA
       Start threadA
   }

   public void OnSomeEvent() {
       do something that changes the views in this activity;
   }

   private class ThreadA extends Thread {
       public void run() {
           do something ...

           Send Some Event to Activity HelloAndroid.
       }
   }
Run Code Online (Sandbox Code Playgroud)

java android

4
推荐指数
1
解决办法
2333
查看次数

标签 统计

android ×9

java ×4

calendar ×1

date ×1