小编nba*_*lle的帖子

RelativeLayout比LinearLayout更昂贵吗?

每次我需要一个View容器时,我一直在使用RelativeLayout,因为它的灵活性,即使我只是想显示一些非常简单的东西.

这样做是否可以,或者我应该从性能/良好实践的角度尝试使用LinearLayout?

谢谢!

performance android android-linearlayout android-relativelayout

113
推荐指数
2
解决办法
4万
查看次数

通过膨胀布局来创建自定义视图?

我正在尝试创建一个自定义视图,它将取代我在多个地方使用的某个布局,但我正在努力这样做.

基本上,我想替换这个:

<RelativeLayout
 android:id="@+id/dolphinLine"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
    android:layout_centerInParent="true"
 android:background="@drawable/background_box_light_blue"
 android:padding="10dip"
 android:layout_margin="10dip">
  <TextView
   android:id="@+id/dolphinTitle"
   android:layout_width="200dip"
   android:layout_height="100dip"
   android:layout_alignParentLeft="true"
   android:layout_marginLeft="10dip"
   android:text="@string/my_title"
   android:textSize="30dip"
   android:textStyle="bold"
   android:textColor="#2E4C71"
   android:gravity="center"/>
  <Button
   android:id="@+id/dolphinMinusButton"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_toRightOf="@+id/dolphinTitle"
   android:layout_marginLeft="30dip"
   android:text="@string/minus_button"
   android:textSize="70dip"
   android:textStyle="bold"
   android:gravity="center"
   android:layout_marginTop="1dip"
   android:background="@drawable/button_blue_square_selector"
   android:textColor="#FFFFFF"
   android:onClick="onClick"/>
  <TextView
   android:id="@+id/dolphinValue"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_marginLeft="15dip"
   android:background="@android:drawable/editbox_background"
   android:layout_toRightOf="@+id/dolphinMinusButton"
   android:text="0"
   android:textColor="#2E4C71"
   android:textSize="50dip"
   android:gravity="center"
   android:textStyle="bold"
   android:inputType="none"/>
  <Button
   android:id="@+id/dolphinPlusButton"
   android:layout_width="100dip"
   android:layout_height="100dip"
   android:layout_toRightOf="@+id/dolphinValue"
   android:layout_marginLeft="15dip"
   android:text="@string/plus_button"
   android:textSize="70dip"
   android:textStyle="bold"
   android:gravity="center"
   android:layout_marginTop="1dip"
   android:background="@drawable/button_blue_square_selector"
   android:textColor="#FFFFFF"
   android:onClick="onClick"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这样:

<view class="com.example.MyQuantityBox"
    android:id="@+id/dolphinBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:myCustomAttribute="@string/my_title"/>
Run Code Online (Sandbox Code Playgroud)

所以,我不想要一个自定义布局,我想要一个自定义视图(这个视图不应该有子视图).

唯一可以从MyQuantityBox的一个实例改为另一个实例的是标题.我非常希望能够在XML中指定它(就像我在最后的XML行上那样)

我怎样才能做到这一点?我应该将RelativeLayout放在/ res/layout中的XML文件中并在MyBoxQuantity类中对其进行充气吗?如果是,我该怎么办?

谢谢!

xml android view

86
推荐指数
5
解决办法
11万
查看次数

Android:如何动态包含XML布局?

我想将我的UI分解为几个XML布局.第一个是主要布局,其他布局是内容布局.

我想可以设置哪些content_layout应该动态地在运行时包括在内,所以我不想设定一个"layout="@+layout/content_layout"在我的XML文件.

这是我的布局:

main_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="600dp"
    android:layout_height="800dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <include /> <!-- I WANT TO INCLUDE MY CONTENT HERE -->

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Cancel" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

content_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/whatever"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)

content_layout2.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/whatever2"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

谢谢!

layout android include android-layout

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

点击浏览XML,使Android按钮更改背景

有没有办法为将要应用的XML文件中的Button指定替代背景图像/颜色onClick,或者我是否必须Button.setBackground()onClickListener

android onclick android-button

34
推荐指数
2
解决办法
14万
查看次数

如何在Python中传递和运行回调方法

我有一个Manager(主线程),它创建其他线程来处理各种操作.我希望我的经理在创建的线程结束时(当run()方法执行完成时)得到通知.

我知道我可以通过Thread.isActive()方法检查所有线程的状态,但轮询很糟糕,所以我想要通知.

我正在考虑给Threads一个回调方法,并在run()方法结束时调用这个函数:

class Manager():
    ...
    MyThread(self.on_thread_finished).start() # How do I pass the callback

    def on_thread_finished(self, data):
        pass
    ...

class MyThread(Thread):
    ...
    def run(self):
        ....
        self.callback(data) # How do I call the callback?
    ...
Run Code Online (Sandbox Code Playgroud)

谢谢!

python notifications multithreading callback

24
推荐指数
2
解决办法
4万
查看次数

如何向下转换Java对象?

我试图理解Java的多态性,我有一个关于向下转换对象的问题.让我们说这个例子我有两个继承自超类Animal的子类Dog和Cat

根据我的理解,向下转换对象的唯一方法是,如果此Object已经是好的类型,如下所示:

Animal a = new Dog();
Dog d = (Dog) a;
Run Code Online (Sandbox Code Playgroud)

这样可行吗?

但是如果我想在不知道它会是什么的情况下创造一个普通的动物,然后在我知道的时候施放它,我该怎么办呢?

Animal a = new Animal();
Dog d = (Dog) a;
Run Code Online (Sandbox Code Playgroud)

这会在运行时抛出ClassCastException吗?

我发现这样做的唯一方法是创建一个新的Dog构造函数,从常规动物创建一个狗:

Animal a = new Animal();
Dog d = new Dog(a);
Run Code Online (Sandbox Code Playgroud)

public Class Dog extends Animal{
   public Dog(Animal a){
      super(a);
   }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,我该怎么做呢?

  • 我这样做是最好的方式吗?
  • 我根本不应该这样做,如果我必须这样做意味着我的计划没有很好地构思?
  • 我错过了更好的方式吗?

非常感谢!nbarraille

java oop inheritance downcast

19
推荐指数
1
解决办法
3万
查看次数

JNI对象创建和内存管理

我有以下JNI方法本地创建Java对象的集合,然后将它们返回到Java:

JNIEXPORT jobject JNICALL Java_com_test_myClass_myMethod(JNIEnv * env, jclass klass) {
    jclass arrayClass = env->FindClass("java/util/ArrayList");
    jmethodID initMethod = env->GetMethodID(arrayClass, "<init>", "()V");
    jmethodID addMethod = env->GetMethodID(arrayClass, "add", "(Ljava/lang/Object;)Z");
    jobject myArray = env->NewObject(arrayClass, initMethod);

    env->CallBooleanMethod(myArray, addMethod, env->NewStringUTF("Hello"));
    env->CallBooleanMethod(myArray, addMethod, env->NewStringUTF("World"));

    return myArray;
}
Run Code Online (Sandbox Code Playgroud)

我是否需要释放在本机代码中创建的对象,还是由GC自动完成?如果我这样做,我该怎么做,因为我需要将它返回到Java?

c++ java java-native-interface memory-leaks memory-management

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

您将如何测试连接池

我在Java中实现了一个非常简单的ConnectionPool.它没有花哨的功能,只是获取/释放连接方法.

我该如何测试它是否有效?

我知道有很多连接池可以在那里使用,它比我要做的更可靠,但我只是想练习理解连接池的工作方式.

谢谢!

这是代码,以防它有帮助:

public class ConnectionPoolImpl implements ConnectionPool {
    private Vector<PooledConnection> connections; // The connections container
    String url;
    String username; 
    String password;

    /**
     * Instanciates a new MySQLConnectionPool
     * @param nbConnectionsMax
     */
    public ConnectionPoolImpl(String DBUrl, String username, String password){
        this.connections = new Vector<PooledConnection>();
        this.url = DBUrl;
        this.username = username;
        this.password = password;
    }

    /**
     * Returns a connection from the pool, if some are available, or create a new one.
     * 
     * @return the connection.
     */
    public Connection …
Run Code Online (Sandbox Code Playgroud)

java testing integration-testing unit-testing connection-pooling

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

为什么我的多线程效率不高?

我设计了一个类,使用不同数量的线程填充整数数组,以便了解多线程的强大功能.但根据我的结果,没有...

这个想法:这个想法太过于填充了100000000个整数,其值为"1".从1个线程开始(一个线程填充整个数组)并将其递增直到100个线程(每个线程填充一个大小为100000000/nbThreads的子数组)

示例:使用10个线程,我创建10个线程,每个线程填充10000000个整数的数组.

这是我的代码:

public class ThreadedArrayFilling extends Thread{
    private int start;
    private int partitionSize;
    public static int[] data;
    public static final int SIZE = 100000000;
    public static final int NB_THREADS_MAX = 100;


    public static void main(String[] args){
        data = new int[SIZE];
        long startTime, endTime;
        int partition, startIndex, j;
        ThreadedArrayLookup[] threads;

        for(int i = 1; i <= NB_THREADS_MAX; i++){       
            startTime = System.currentTimeMillis();
            partition = SIZE / i;
            startIndex = 0;
                threads = new ThreadedArrayLookup[i];
            for(j = 0; j …
Run Code Online (Sandbox Code Playgroud)

java performance benchmarking multithreading

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

带有九个补丁文件的Eclipse bug?

我试图将我的所有按钮背景转换为.9.png,但是我遇到了Eclipse的问题.

我使用该工具创建了我的九个补丁.png,但是因为我把它放在我的/ res/drawable文件夹中,当我尝试访问以@drawable开头的任何资源时,我的所有.xml文件中都有以下错误:

Error: No resource found that matches the given name (at 'drawable' with value '@drawable/my_resource').
Run Code Online (Sandbox Code Playgroud)

这是我的.9.png: 九个补丁

这是一个截图: 截图

我的.9.png中是否有问题或是否是eclipse的问题?

谢谢

eclipse resources android nine-patch

9
推荐指数
1
解决办法
4831
查看次数